安卓:MediaPlayer的完成而不释放 [英] Android: MediaPlayer finalized without being released

查看:1231
本文介绍了安卓:MediaPlayer的完成而不释放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我有一个有点过分张贴code,因为我包了几个对象,允许状态进行管理(在MediaPlayer类目前不提供状态信息),以及其他各种原因,但code是pretty的多少:

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);
}

通过这种方法在我SoundManager类类:

With this method in my SoundManager class:

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

而在initialiseSounds我做的:

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

当我要玩我做的曲目:

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

鸭preciate任何意见,

Appreciate any advice,

谢谢

推荐答案

我不能完全确定该消息的来源,但这里要注意的是创建两个 MediaPlayers:一个在的onCreate,一个在onResume。

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.

这消息似乎表明,它不喜欢一个MP正在定稿(GC'd)而不被释放。它可能是指你的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.

也许错误会自动消失,如果你只创建一个MediaPlayer的,而不是两个?

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

这篇关于安卓:MediaPlayer的完成而不释放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆