播放声音的垃圾邮件按钮

时间:2018-03-20 23:57:28

标签: java android button audio

我需要帮助弄清楚如何制作一个会一遍又一遍地播放相同声音的按钮,而不必等待声音完成播放。

这是我目前的代码。

final MediaPlayer mp = MediaPlayer.create(this,R.raw.boosto);


Button button = (Button) this.findViewById(R.id.button);

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {


            mp.start();

        }

2 个答案:

答案 0 :(得分:2)

SoundPool

SoundPool包含一组源音乐,声源可以来自应用程序或文件系统中的音乐文件,... SoundPool支持同时播放音乐源。

使用方法:

https://developer.android.com/reference/android/media/SoundPool.html

SoundPool sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);

 /** soundId for Later handling of sound pool **/
 int soundId = sp.load(context, R.raw.windows_8_notify, 1); 

 sp.play(soundId, 1, 1, 0, 0, 1);

示例:

public class MainActivity extends AppCompatActivity {


   private SoundPool soundPool;

   private AudioManager audioManager;

   // Maximumn sound stream.
   private static final int MAX_STREAMS = 5;

   // Stream type.
   private static final int streamType = AudioManager.STREAM_MUSIC;

   private boolean loaded;

   private int soundIdDestroy;
   private int soundIdGun;
   private float volume;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

       // AudioManager audio settings for adjusting the volume
       audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);

       // Current volumn Index of particular stream type.
       float currentVolumeIndex = (float) audioManager.getStreamVolume(streamType);

       // Get the maximum volume index for a particular stream type.
       float maxVolumeIndex  = (float) audioManager.getStreamMaxVolume(streamType);

       // Volumn (0 --> 1)
       this.volume = currentVolumeIndex / maxVolumeIndex;

       // Suggests an audio stream whose volume should be changed by
       // the hardware volume controls.
       this.setVolumeControlStream(streamType);

       // For Android SDK >= 21
       if (Build.VERSION.SDK_INT >= 21 ) {

           AudioAttributes audioAttrib = new AudioAttributes.Builder()
                   .setUsage(AudioAttributes.USAGE_GAME)
                   .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                   .build();

           SoundPool.Builder builder= new SoundPool.Builder();
           builder.setAudioAttributes(audioAttrib).setMaxStreams(MAX_STREAMS);

           this.soundPool = builder.build();
       }
       // for Android SDK < 21
       else {
           // SoundPool(int maxStreams, int streamType, int srcQuality)
           this.soundPool = new SoundPool(MAX_STREAMS, AudioManager.STREAM_MUSIC, 0);
       }

       // When Sound Pool load complete.
       this.soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
           @Override
           public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
               loaded = true;
           }
       });

       // Load sound file (destroy.wav) into SoundPool.
       this.soundIdDestroy = this.soundPool.load(this, R.raw.destroy,1);

       // Load sound file (gun.wav) into SoundPool.
       this.soundIdGun = this.soundPool.load(this, R.raw.gun,1);

   }



   // When users click on the button "Gun"
   public void playSoundGun(View view)  {
     if(loaded)  {
         float leftVolumn = volume;
         float rightVolumn = volume;
         // Play sound of gunfire. Returns the ID of the new stream.
         int streamId = this.soundPool.play(this.soundIdGun,leftVolumn, rightVolumn, 1, 0, 1f);
     }
   }

   // When users click on the button "Destroy"
   public void playSoundDestroy(View view)  {
       if(loaded)  {
           float leftVolumn = volume;
           float rightVolumn = volume;

           // Play sound objects destroyed. Returns the ID of the new stream.
           int streamId = this.soundPool.play(this.soundIdDestroy,leftVolumn, rightVolumn, 1, 0, 1f);
       }
   }

}

答案 1 :(得分:1)

当播放已经开始时,对start()的通话不起作用。使用seekTo()将播放重置为开头。

public void onClick(View v) {
    if(mp.isPlaying())
        mp.seekTo(0L, MediaPlayer.SEEK_NEXT_SYNC); // continues playback from millisecond 0
    else
        mp.start();
}