【问题标题】:Mediaplayer stops working after a while媒体播放器在一段时间后停止工作
【发布时间】:2017-03-01 12:34:10
【问题描述】:

我有一个音板,一切正常,但是当我在一段时间后单击按钮时,声音刚刚停止播放..这是代码:

    public class MainActivity extends AppCompatActivity{


        //Sounds/Remixes MediaPlayer
        MediaPlayer sound1, sound2, sound3, sound4, remix1, remix2, remix3, remix4;

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


            //Sound MediaPlayers
            sound1= MediaPlayer.create(this, R.raw.sound1);
            sound2= MediaPlayer.create(this, R.raw.sound2);
            sound3= MediaPlayer.create(this, R.raw.sound3);
            sound4= MediaPlayer.create(this, R.raw.sound4);



            //Remixes MediaPlayer
            remix1= MediaPlayer.create(this, R.raw.remix1);
            remix2= MediaPlayer.create(this, R.raw.remix2);
            remix3= MediaPlayer.create(this, R.raw.remix3);
            remix4= MediaPlayer.create(this, R.raw.remix4);




        }

    //Songs stop playing when closing/switching the App
        @Override
        protected void onPause() {
            remix1.pause();
            remix2.pause();
            remix3.pause();
            remix4.pause();
            super.onPause();
        }


    //Remix onClick's
    public void remix1(View view){
        if(remix1.isPlaying() == true){
            remix1.pause();
        }else{
            remix1.start();
        }
    }
    public void remix2(View view){
        if(remix2.isPlaying() == true){
            remix2.pause();
        }else{
            remix2.start();
        }
    }
    public void remix3(View view){
        if(remix3.isPlaying() == true){
            remix3.pause();
        }else{
            remix3.start();
        }
    }
    public void remix4(View view){
        if(remix4.isPlaying() == true){
            remix4.pause();
        }else{
            remix4.start();
        }
    }




    //sound onClick's 
    public void sound1(View view){
        sound1.start();
    }
    public void sound2(View view){
        sound2.start();
    }
    public void sound3(View view){
        sound3.start();
    }
    public void sound4(View view){
        sound4.start();
    }

}

这是我的代码,只有 4 个声音和 4 个混音。

声音时长 3 秒,混音时长 3 分钟。

这是我的应用程序,您可以尝试一下,有时您会看到按钮停止工作:https://play.google.com/store/apps/details?id=com.aaron.waller.angelasoundboard

我找到的唯一解决方案是每次使用后清理 MediaPlayer,我该怎么做?

【问题讨论】:

标签: android button audio onclick media-player


【解决方案1】:

要在每次使用后清理媒体播放器,您应该调用

mediaPlayer.release();

但如果你需要的只是在 create 方法之后将 mediaPlayer 返回到它的状态

mediaPlayer.reset();

应该够了

更新

对于您使用的每个 mediaPlayer,将此代码添加到 onCreate

mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
    public void onCompletion(MediaPlayer mp) {
        mp.reset();
        //if reset doesnt give you what you need use mp.release() instead
        //but dont forget to call MediaPlayer.create  
        //before next mediaPlayer.start() method

    }
});

【讨论】:

  • @AaronWaller 不,在确定停止或 onCompletion 之后
  • 请给我一个例子,我不明白,因为我是 Android Studio 的新手。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-06
  • 1970-01-01
相关资源
最近更新 更多