简体   繁体   中英

Android: MediaPlayer finalized without being released

I'm using the MediaPlayer class in an app that I'm currently working on. I want to hold on to an instance of the MediaPlayer class for the lifetime of my Activity. I'm releasing the resources from the MediaPlayer class in the onPause() { } method of the activity, however when the activity starts I see the following warning pop up in the LogCat window:

W/MediaPlayer-JNI(16795): MediaPlayer finalized without being released

Any idea what I'm doing wrong here or how to remove this warning? It doesn't appear to cause any issues and the sound works just fine.

I have a bit too much code to post because I wrapped a few objects to allow state to be managed (the MediaPlayer class doesn't currently provide state information), and various other reasons, but the code is pretty much:

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    initialiseSounds(this);
}

@Override
protected void onPause()
{
    soundManager.releaseResources();
}

@Override
protected void onResume()
{
    initialiseSounds(this);
}

With this method in my SoundManager class:

public void releaseResources()
{
    player.release();
}

And in initialiseSounds I'm doing:

MediaPlayer player = new MediaPlayer();
player.reset();
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
AssetFileDescriptor assetFileDescriptor = context.getResources().openRawResourceFd(resourceId);
setDataSource(player, assetFileDescriptor);

When I want to play the track I do:

player.prepare();
player.start();

Appreciate any advice,

Thanks,

I'm not completely sure of the origin of that message, but notice here that you're creating two MediaPlayers: one in onCreate and one in onResume.

That message seems to indicate that it doesn't like that a MP is being finalized (GC'd) without being 'released'. It may be referring to that first mediaPlayer you create in onCreate, which is lost after you reassign it in onResume.

Perhaps the error will go away if you only create one MediaPlayer instead of two?

This message also appears if you don't call release on the media player object and your fragment is stopped, and destroyed.

You need to override onStop() or one of the other methods and call release() on the media player.

@Override
public void onStop() {
    super.onStop();
    mediaPlayer.stop();
    mediaPlayer.release();
}

in my case, I have created local media player reference in a method and what I did was that all the time when I want to play some audio I had used that method. but then I figured that after sudden that local variable is gonna clean from the memory and MediaPlayer can't be accessed anymore with that reference.

Then I created a one global static Mediaplayer reference and when ever i create a media player object i initilized it to that static variable and my problem seems to be solved

我遇到了类似的问题 在 onStart() 或 onResume 中调用方法,不要在 onCreate() 中调用它。

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