2017-03-01 101 views
-2

我有一个音板,一切工作正常,但是当我经过很多点击的按钮,而声音刚刚停止播放..这是代码:媒体播放器一段时间后停止工作

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,我该怎么办呢?

+0

你的onClick看起来很奇怪,他们叫别的地方吗?我期望看到更多的东西在这一行:http://stackoverflow.com/questions/18459122/play-sound-on-button-click-android – Phil3992

回答

0

要清理的媒体播放器每次使用后,你应该叫

mediaPlayer.release(); 

但是如果你需要之后create方法只是返回的MediaPlayer其状态一切

mediaPlayer.reset(); 

应该足够

UPD

在你身边r case将此代码添加到您使用的每个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 

    } 
}); 
+0

像这样? http://pastebin.com/KPVhJUzt –

+0

@AaronWaller没有,肯定还是在完成之后 – DEADMC

+0

请给我一个例子,我不明白它在Android Studio中是否是新的。 –