Android:播放音频剪辑 onClick

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3041763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:26:34  来源:igfitidea点击:

Android: Playing an audio clip onClick

androidaudio

提问by Sachin

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

谢谢

回答by Cristian

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.slayerrepresents an audio file called slayer.mp3that 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/文件存储在文件夹中,一旦您单击按钮,机器人就会震撼您...

回答by Aashish

You can also achieve the same using SoundPool.

您也可以使用SoundPool.

MediaPlayerfirst loads the whole sound data in memory then play, so it produces some lag when we switch among sounds frequently.

MediaPlayer首先将整个声音数据加载到内存中然后播放,因此当我们在声音之间频繁切换时会产生一些滞后。

SoundPoolis a better option with small size sound file and produces better result with .oggmedia file.

SoundPool是小尺寸声音文件的更好选择,并且对.ogg媒体文件产生更好的效果。

SoundPool pl = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
        // 5 indicates the maximum number of simultaneous streams for this SoundPool object
pl.setOnLoadCompleteListener(new OnLoadCompleteListener() {             
            @Override
            public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
                // The onLoadComplet method is called when a sound has completed loading.
                soundPool.play(sampleId, 1f, 1f, 0, 0, 1);
                // second and third parameters indicates left and right value (range = 0.0 to 1.0)
            }
});

Button btn = findViewById(R.id.boton);
btn.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View v) {

     int sound = pl.load(this, R.raw.sound_01, 0);

 }
});

回答by Rohith S

public void aud_play(View view) {
  if (!mp.isPlaying()) {   //If media player is not playing it.

    mp = MediaPlayer.create(this, R.raw.audio_name);
    mp.start();      

  } else {// Toast of Already playing ...
}
}