1

i created a MediaPlayer on adapter

 void setData(Users data) {
            mediaPlayer = new MediaPlayer();
            if (!data.getAudioIntro().equals("")) {
                try {
                    mediaPlayer.setDataSource(data.getAudioIntro());
                    mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                        @Override
                        public void onPrepared(MediaPlayer mp) {
                            booleanCallBack.checkAudio(true);
                            username.setText(data.getUsername());
                            Glide.with(context).load(data.getAvatar()).into(imageView);
                        }
                    });
                    mediaPlayer.prepareAsync();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!data.getAudioIntro().equals("")) {
                        if (mediaPlayer.isPlaying())
                            mediaPlayer.pause();
                        else {
                            mediaPlayer.start();
                        }
                    } else {
                        Toast.makeText(context, "No Voice intro", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }

It work perfectly. then i create method to stop Audio

public void stopMediaplayer() {
        if (mediaPlayer.isPlaying()) {
            mediaPlayer.stop();
            mediaPlayer.release();
        } else {
            mediaPlayer.stop();
            mediaPlayer.release();
        }
    }

on Fragment, i call this method when CardView Swiped

           @Override
            public void onCardSwiped(Direction direction) {
                adapter.stopMediaplayer();                      
            }

but i get the error

E/MediaHTTPConnection: java.lang.NullPointerException: Attempt to invoke virtual method 'java.net.URLConnection java.net.URL.openConnection()' on a null object reference
        at android.media.MediaHTTPConnection.seekTo(MediaHTTPConnection.java:256)
        at android.media.MediaHTTPConnection.getMIMEType(MediaHTTPConnection.java:479)
        at android.media.IMediaHTTPConnection$Stub.onTransact(IMediaHTTPConnection.java:159)
        at android.os.Binder.execTransactInternal(Binder.java:1021)
        at android.os.Binder.execTransact(Binder.java:994)

and another error

/MediaHTTPConnection: java.io.IOException: Canceled
        at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:172)
        at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:144)
        at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:106)
        at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:400)
        at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:333)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:483)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:429)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:560)
        at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:106)
        at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:30)
        at android.media.MediaHTTPConnection.seekTo(MediaHTTPConnection.java:292)
        at android.media.MediaHTTPConnection.getMIMEType(MediaHTTPConnection.java:479)
        at android.media.IMediaHTTPConnection$Stub.onTransact(IMediaHTTPConnection.java:159)
        at android.os.Binder.execTransactInternal(Binder.java:1021)
        at android.os.Binder.execTransact(Binder.java:994)

how can i fix it , please help me!

This life is not easy, but if every day we give our best effort we can make all our dreams. I wish you well on this day.

1 Answers1

0

If it worked before you added the stopMediaPlayer function, then your issues are probably coming from there. Going off the docs:

When a MediaPlayer object is just created using new or after reset() is called, it is in the Idle state; and after release() is called, it is in the End state. Between these two states is the life cycle of the MediaPlayer object.

It is also recommended that once a MediaPlayer object is no longer being used, call release() immediately so that resources used by the internal player engine associated with the MediaPlayer object can be released immediately. Resource may include singleton resources such as hardware acceleration components and failure to call release() may cause subsequent instances of MediaPlayer objects to fallback to software implementations or fail altogether. Once the MediaPlayer object is in the End state, it can no longer be used and there is no way to bring it back to any other state.

Your stopMediaPlayer code always releases the player, at which point it's useless. isPlaying throws an exception if you call it on a player that's been released, so you need to also check if the player's actually valid before doing anything with it (maybe set it to null after release)

Your other errors could be anything, I'd worry about fixing this before trying to diagnose them - they're probably just consequences of trying to use the released player, if it all worked fine before you added that tiny bit of code!

cactustictacs
  • 17,935
  • 2
  • 14
  • 25
  • i reading the doc and i still don't know how to fix my problem, it complex than i thinked, but thanks for your helping! – Lộc Trần Phước Aug 12 '21 at 03:28
  • @LộcTrầnPhước If you want to reuse that media player (e.g. calling ``play`` on it later), just don't ``release`` it. If you *do* release it, you can't use it anymore, so make sure you create a new one when you need to ``play`` some sound. You'll need to release it somewhere (maybe in ``onStop``) but that really depends on what you'r app's doing – cactustictacs Aug 12 '21 at 17:42