1

我试图让媒体播放器在播放下一个声音之前停止播放声音。所以声音不会互相穿过。这是我的第一个应用程序,我认为我应该从制作音板开始。

这就是我现在所拥有的:

    final MediaPlayer mediaPlayer = new MediaPlayer();
    // import sound files
    final MediaPlayer sound01 = MediaPlayer.create(this, R.raw.sound01);
    final MediaPlayer sound02 = MediaPlayer.create(this, R.raw.sound02);
    final MediaPlayer sound03 = MediaPlayer.create(this, R.raw.sound03);
    final MediaPlayer sound04 = MediaPlayer.create(this, R.raw.sound04);
    final MediaPlayer sound05 = MediaPlayer.create(this, R.raw.sound05);
    final MediaPlayer sound06 = MediaPlayer.create(this, R.raw.sound06);
    final MediaPlayer sound07 = MediaPlayer.create(this, R.raw.sound07);
    final MediaPlayer sound08 = MediaPlayer.create(this, R.raw.sound08);
    final MediaPlayer sound09 = MediaPlayer.create(this, R.raw.sound09);
    final MediaPlayer sound10 = MediaPlayer.create(this, R.raw.sound10);
    final MediaPlayer sound11 = MediaPlayer.create(this, R.raw.sound11);
    final MediaPlayer sound12 = MediaPlayer.create(this, R.raw.sound12);
    final MediaPlayer sound13 = MediaPlayer.create(this, R.raw.sound13);
    final MediaPlayer sound14 = MediaPlayer.create(this, R.raw.sound14);
    final MediaPlayer sound15 = MediaPlayer.create(this, R.raw.sound15);
    final MediaPlayer sound16 = MediaPlayer.create(this, R.raw.sound16);
    final MediaPlayer sound17 = MediaPlayer.create(this, R.raw.sound17);
    final MediaPlayer sound18 = MediaPlayer.create(this, R.raw.sound18);
    final MediaPlayer sound19 = MediaPlayer.create(this, R.raw.sound19);
    final MediaPlayer sound20 = MediaPlayer.create(this, R.raw.sound20);
    final MediaPlayer sound21 = MediaPlayer.create(this, R.raw.sound21);
    final MediaPlayer sound22 = MediaPlayer.create(this, R.raw.sound22);
    final MediaPlayer sound23 = MediaPlayer.create(this, R.raw.sound23);
    final MediaPlayer sound24 = MediaPlayer.create(this, R.raw.sound24);
    final MediaPlayer sound25 = MediaPlayer.create(this, R.raw.sound25);


    // play sound files on clicks
    Button s01 = (Button) findViewById(R.id.btn1_1); 
    s01.setText(this.getString(R.string.button1_1));
    s01.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            try {
                sound01.prepare();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            mediaPlayer.stop();
            sound01.start();                
            }

    });
    registerForContextMenu(s01);

提前致谢!

4

2 回答 2

0

您只需要一个 MediaPlayer;您绝对不需要加载多个实例。每个按钮都会调用sound.stop();,然后加载下一个声音。

于 2013-05-12T15:02:41.547 回答
-1

不,您已经创建了 25 个不同的 MediaPlayer 实例。没有停止播放的静态方法。最好的方法是自己跟踪当前正在播放的音乐,并在需要时停止播放。

实际上,为此目的,您可能最好使用SoundPool,它会为您提供一个对象实例来管理您的所有声音,而不是每个声音一个播放器。

于 2013-05-12T15:03:40.417 回答