MediaPlayer是android官方提供的一组音频/视频播放Api, 可以满足简单的、大部分音频/视频文件的播放需求。由于最近项目需要音频播放,第一次在Android APP引入了MediaPlayer。初步开发实现过程还算顺利,现在最终上线测试过程中,发现音频播放大概率出现中断不播完的问题,但是有时候又正常。经过反复的调试与问题追踪,最终发现每次播放中断,日志都会输出以下内容:
04-06 15:56:14.713 11324-11332/com.ivan.ivanapp W/MediaPlayer-JNI: MediaPlayer finalized without being released
04-06 15:56:35.819 11324-11332/com.ivan.ivanapp W/MediaPlayer-JNI: MediaPlayer finalized without being released
从日志内容可以看出,MediaPlayer jni底层调用出现了警告信息,大概意思是:MediaPlayer资源没有释放就结束了。为了进一步分析问题原因,在尝试各种方案和查询官方和stackoverflow论坛资料后,终于发现有价值的解决方案。以下引用自stackoverflow:
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.
This means, it can be free'd by the GC before it ha