Play Button Doesn't Work After Music Is Stopped With Stop Button
I have two buttons that will start and stop a song, when i start the app I can play the song and then stop it again. However when i wanna start it again it doesn't work. final
Solution 1:
I guess you want to start again by play button. Add the following code above the mp.start()
method:
mp.reset();
mp.prepare();
mp.start();
I guess you have rest of things set properly and in stop button use the following:
if(mp!=null) {
mp.stop();mp.release();mp=null;
}
Solution 2:
in play.onclicklistner
mp = MediaPlayer.create(Peshmerga.this, R.raw.sppeshmerga);
mp.start();
add following to your stop.onclicklistner
if(mp!=null) {
mp.stop();mp.release();mp=null;
}
you need to create everytime you click play button .
Solution 3:
Look at the state diagram image in the following link:
-----------------------------------------android-mediaplayer-sample-code----------------------------------------------
mp.reset();
mp.prepare();
mp.start();
Solution 4:
The Play Button Click Listener:
playButton.setOnClickListener(newView.OnClickListener(){
@OverridepublicvoidonClick(View v){
try{
if(!mMediaPlayer.isPlaying()){
mMediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.test);
mMediaPlayer.start();
}
}
catch (Exception e){
}
}
});
Stop Button Click Listener:
stopButton.setOnClickListener(newView.OnClickListener(){
@OverridepublicvoidonClick(View v){
if (mMediaPlayer != null) {
mMediaPlayer.stop();
mMediaPlayer.release();
mMediaPlayer = null;
mMediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.test);
}
}
});
Post a Comment for "Play Button Doesn't Work After Music Is Stopped With Stop Button"