Android:MediaPlayer 定稿未发布

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12590435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:08:00  来源:igfitidea点击:

Android: MediaPlayer finalized without being released

androidandroid-mediaplayer

提问by magritte

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:

我在我目前正在开发的应用程序中使用 MediaPlayer 类。我想在我的 Activity 的生命周期内保留 MediaPlayer 类的一个实例。我正在活动的 onPause() { } 方法中释放 MediaPlayer 类中的资源,但是当活动开始时,我看到 LogCat 窗口中弹出以下警告:

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:

我要发布的代码有点多,因为我包装了一些对象以允许管理状态(MediaPlayer 类当前不提供状态信息)以及其他各种原因,但代码几乎是:

@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:

在我的 SoundManager 类中使用此方法:

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

And in initialiseSounds I'm doing:

在 initialiseSounds 中,我正在做:

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,

谢谢,

回答by Tim

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

我不完全确定该消息的来源,但请注意,您正在创建两个MediaPlayers:一个在 onCreate 中,一个在 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.

该消息似乎表明它不喜欢在没有“发布”的情况下最终确定(GC'd)MP。它可能指的是您在 onCreate 中创建的第一个 mediaPlayer,在您在 onResume 中重新分配它后丢失了。

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

如果您只创建一个 MediaPlayer 而不是两个,错误可能会消失?

回答by Dhiraj Gupta

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

如果您不在媒体播放器对象上调用 release 并且您的片段被停止并销毁,也会出现此消息。

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

您需要覆盖onStop()或其他方法之一并在媒体播放器上调用 release() 。

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

回答by Lasitha Lakmal

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.

就我而言,我已经在一种方法中创建了本地媒体播放器引用,而我所做的就是在我想播放某些音频时一直使用该方法。但后来我发现,突然之间,局部变量将从内存中清除,并且不能再使用该引用访问 MediaPlayer。

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

然后我创建了一个全局静态 Mediaplayer 引用,每当我创建一个媒体播放器对象时,我都会将它初始化为该静态变量,我的问题似乎得到了解决

回答by Ricky

I have met similar problem Call the method in onStart() or onResume , Do not call it in onCreate().

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