且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

垃圾收集原因:MediaPlayer 已完成但未发布

更新时间:2023-12-05 11:06:34

我认为这是因为您在方法的范围内创建了媒体播放器,因此,当方法完成时,它超出了范围.这意味着没有引用,因此可以进行垃圾收集.

I think this is because you create the media player within the scope of the method, therefore, when the method completes, it goes out of scope. This means there are no references, so is ok for garbage collection.

这意味着,它甚至可以在调用 onCompletion 之前被 GC 释放,因此在清除之前不会释放.相反,您需要将媒体播放器的引用存储为类中的成员变量.

This means, it can be free'd by the GC before it has even called onCompletion, hence won't release before cleared. Instead, you need to store a reference to the media player as a member variable in your class.