简体   繁体   中英

Media Player doesn't play for the second time

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.

https://developer.android.com/reference/android/media/MediaPlayer img ref

Problem

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)

Solution

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.).

Suggestions

  1. Always create MediaPlayer instance in onCreate() .
  2. Release MediaPlayer instance in onDestroy() Use in between.
  3. Use 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM