Robert
Robert

Reputation: 4820

Unexpected behaviour of Android MediaPlayer.OnCompletionListener()

I have run into a behaviour of the Android's MediaPlayer.OnCompletionListener() that I cannot explain and I wish that someone could explain for to me.

I have implemented a utility class which is responsible for all handling with the MediaManager in my application. The use case is that I create and prepare a MediaPlayerobject and once it has completed playback the MediaPlayer is reset(), released() and set to null just to ensure that the garbage collector take care of it as soon as possible.

This releasing and nullifying all occurrs within the mediaplayers onCompletionListener, and when I do reset(), release() and null on the MediaPlayer passed in as argument to the onCompletion() method the reset() and release() operation goes fine but it cannot be set to null. However if I use the parent classes instance of MediaPlayer then reset(), release() and nullifying goes fine. Now my question is, what is the difference? It's the same object.

An example to clarify the problem.

public class SoundHandler {
    private MediaPlayer mediaPlayer;

    playSignal(Context context) {
        mediaPlayer = new MediaPlayer();

        // ...Further preparation of mediaPlayer omitted

        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                mp.reset(); // Works
                mp.release(); // Works
                mp = null; // Fails, contains the same memory reference as before...
            }
        });
    }
}

public class SoundHandler {
    private MediaPlayer mediaPlayer;

    playSignal(Context context) {
        mediaPlayer = new MediaPlayer();

        // ...Further preparation of mediaPlayer omitted

        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                mediaPlayer.reset(); // Works
                mediaPlayer.release(); // Works
                mediaPlayer= null; // Works, object is null after this assignment
            }
        });
    }
}

I don't see what the difference is and I would really like to have this explained.

Thanks!

Upvotes: 0

Views: 221

Answers (1)

GreyBeardedGeek
GreyBeardedGeek

Reputation: 30088

In the first case, the MediaPlayer being set to null is an argument to the method. Java uses 'pass by reference' semantics, so the argument to your method is just a copy of the SoundHandler's reference to the object, not the SoundHandler's own reference to it.

So, you can think of the argument to your method as a local variable. When you set it to null, the effect is only local (within your method). The SoundHandler still has it's own reference.

In the second case, however, the onCompletionListener is directly setting the SoundHandler's reference to the MediaPlayer to null. It can do this because the onCompletionListener is an anonymous inner class of the SoundHandler.

Upvotes: 1

Related Questions