android media player.release,内存回收之Android MediaPlayer音频播放异常中断分析

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

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package com.music.zds; import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.os.Binder; import android.os.Bundle; import android.os.IBinder; import android.os.Message; import android.util.Log; import java.util.Timer; import java.util.TimerTask; public class MusicService extends Service { public static final String TAG="MusicService"; public MediaPlayer player; private Timer timer; public MusicService() { Log.d(TAG,"MusicService"); } @Override public void onCreate() { super.onCreate(); player=new MediaPlayer(); } @Override public IBinder onBind(Intent intent) { Log.d(TAG,"MusicService()"); return new MusicControler(); } public void addTimer(){ if(timer ==null){ TimerTask task = new TimerTask() { @Override public void run() { if(player == null) return;; int duration = player.getDuration(); int currentPro = player.getCurrentPosition(); Log.w(TAG,"duration:"+duration+",progress:"+currentPro); Message msg = MainActivity.handler.obtainMessage(); Bundle bundle = new Bundle(); bundle.putInt("duration",duration); bundle.putInt("progress",currentPro); msg.setData(bundle); MainActivity.handler.sendMessage(msg); } }; timer.schedule(task,5,500); } } class MusicControler extends Binder{ public void play(){ Log.d(TAG,"play"); try{ player.reset(); player=MediaPlayer.create(getApplicationContext(),R.raw.nianlun,R.raw.jimoyanhuo); player.start(); addTimer(); } catch (Exception e){ e.printStackTrace(); } } public void pause(){ Log.d(TAG,"pause"); player.pause(); } public void continueplay(){ Log.d(TAG,"continueplay"); player.start(); } public void seekTo(int progress){ Log.d(TAG,"seekTo"+progress); player.seekTo(progress); } } @Override public void onDestroy(){ super.onDestroy(); if (player == null) return;; if(player.isPlaying())player.stop(); player.release(); player=null; } }
最新发布
06-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值