正在开发一个应用程序,当用户触摸图像时播放声音。 我创建自己的声音文件。在这种情况下,我有3个疑惑,
我需要在哪个文件夹中放置声音文件。??
哪种方法适合播放声音..
如何检测用户触摸图像的位置,因为当用户触摸不同的地方时我需要播放不同的声音。
希望你理解我的想法.......请帮助我......
public boolean onTouch(View v, MotionEvent event)
{
touchEvent(event);
return true;
}
private void touchEvent(MotionEvent event)
{
}
答案 0 :(得分:1)
1。您应将声音文件放在/res/raw
文件夹中。
2. 使用以下代码播放声音
MediaPlayer mp = MediaPlayer.create(activity,R.raw.sound_file);
mp.start();
3。附加TouchListener
将允许您处理MotionEvent
,其中包含实际的触摸位置;只需执行以下操作
if(event.getX() < image.width / 2) {
// touch on left
} else {
// touch on right;
}
您还可以通过添加更多逻辑来进一步优化您的触摸位置。
答案 1 :(得分:0)
我需要在哪个文件夹中放置声音文件。??
raw folder
如果不在res中,则创建一个。
答案 2 :(得分:0)
res / raw文件夹中的声音文件
首先,获取x和y坐标,然后将条件设置为触控并尝试使用以下代码播放音乐
MediaPlayer mPlayer2;
mPlayer2= MediaPlayer.create(this, R.raw.music);
mPlayer2.start();
答案 3 :(得分:0)
In which folder i need to place sound file.??
通常文件(mp3,csv,pdf)文件放在Raw
文件夹中。
Which method is suitable for playing sounds..
没有这么简单的方法。您需要使用MediaPlayer class
how can i detect where the user touch on the image, because i need to play different sound when user touches at different places.
向ImageView添加onClickEventListener
并从那里调用MediaPlayer实例。
答案 4 :(得分:0)
我认为您可以使用Event OnClick
MediaPlayer song;
image.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
song=MediaPlayer.create(this, R.raw.songname);
song.start();
}
});