开发者

how can i play small music file when i tuch an image in android?

I am New to android, My task is to pla开发者_如何学运维ying a small audio file when we touch an image?


Depending on your target API version there are a different ways of doing this.

Assuming you have something like this inside your layout xml file.

<ImageView
    android:id="@+id/ImageThatWillPlaySound"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    androidL:onClick="playSound">
</ImageView>

Note androidL:onClick="playSound" which will run the function in your activity that matches the definition:

public void playSound(View v) {
}

Inside that function, you would use MediaPlayer as xandy suggested.

Something like this

public void playSound(View v) {
    MediaPlayer mp = MediaPlayer.create(context, R.raw.sound_file);
    mp.start();
}

Where inside your assets folder you had a file called sound_file.


Use MediaPlayer to play.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜