Android:在onClick上播放音频片段

时间:2022-02-26 20:25:01

How do I set up an audiofile to play when a user touches an image.

如何设置用户触摸图像时播放的音频文件。

Where should I store the audio file and what code should I use to actually play the file? I don't want to bring up the MediaPlayer interface or anything like that.

我应该在哪里存储音频文件以及我应该用什么代码来实际播放文件?我不想提出MediaPlayer界面或类似的东西。

I was thinking of doing it like this:

我想这样做:

foo = (ImageView)this.findViewById(R.id.foo);
    foo.setOnClickListener(this);

public void onClick(View v) {
if (foo.isTouched()) {

 playAudioFile();
  }
}

Thanks

1 个解决方案

#1


55  

This won't create a bring up the MediaPlayer interface... it will just play the sound you want.

这不会创建一个MediaPlayer界面...它只会播放你想要的声音。

Button boton = (Button) findViewById(R.id.boton);
boton.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View v) {
  MediaPlayer mp = MediaPlayer.create(TestSonido.this, R.raw.slayer);  
  mp.start();
 }
});

In this case, R.raw.slayer represents an audio file called slayer.mp3 that is stored in the res/raw/ folder and once you click the button the droid will rock you...

在这种情况下,R.raw.slayer表示一个名为slayer.mp3的音频文件,存储在res / raw /文件夹中,一旦你点击按钮,droid就会摇滚你......

#1


55  

This won't create a bring up the MediaPlayer interface... it will just play the sound you want.

这不会创建一个MediaPlayer界面...它只会播放你想要的声音。

Button boton = (Button) findViewById(R.id.boton);
boton.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View v) {
  MediaPlayer mp = MediaPlayer.create(TestSonido.this, R.raw.slayer);  
  mp.start();
 }
});

In this case, R.raw.slayer represents an audio file called slayer.mp3 that is stored in the res/raw/ folder and once you click the button the droid will rock you...

在这种情况下,R.raw.slayer表示一个名为slayer.mp3的音频文件,存储在res / raw /文件夹中,一旦你点击按钮,droid就会摇滚你......