I use same button for play and pause.It can handle play and pause smoothly.But after the music file end, it can not play it again.When I press it restart the application.I use mp.reset(); mp.release();.It doesn't help me in the case
Java Code:
final MediaPlayer mp1;
mp1 = MediaPlayer.create(convertView.getContext().getApplicationContext(), convertView.getResources().getIdentifier(audiopath, "raw", convertView.getContext().getPackageName()));
mHolder.play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mp1.isPlaying()) {
mp1.pause();
mHolder.play.setImageResource(R.drawable.plays);
} else {
mp1.start();
mHolder.play.setImageResource(R.drawable.pause);
mp1.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.reset();
mp.release();
mHolder.play.setImageResource(R.drawable.plays);
}
});
}
}
});
LogCat:
java.lang.IllegalStateException
at android.media.MediaPlayer.isPlaying(Native Method)
at com.example.package.adapter.AdapterN$3.onClick(AdapterN.java:223)
Its probably because of the "mp.release()":
As you can see in the documentation here , it state that after release(), the object is no longer available.
So, what i would suggest here is you may just remove the "mp.release()" and put it under onDestroy() of your activity.
When you call release()
of a MediaPlayer
, it deallocates all its resources allocated with MediaPlayer.create()
previously, hence no longer being accessible. This produces,
java.lang.IllegalStateException at android.media.MediaPlayer.isPlaying(Native Method) at com.example.package.adapter.AdapterN$3.onClick(AdapterN.java:223)
You should remove,
mp.release()
However, you can still use release()
but in that case you have to again create MediaPlayer
instance using,
mp1 = MediaPlayer.create(convertView.getContext().getApplicationContext(), convertView.getResources().getIdentifier(audiopath, "raw", convertView.getContext().getPackageName()));
in proper place (ie before accessing any start, pause, reset etc.).
MediaPlayer
instance in onCreate()
. onDestroy()
Use in between. start/pause/reset
APIs in between create and release.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.