android media player.release,android的mediaplayer在release后一定需要=null

z转自:xxx(暂时忘记了)

Apparently the documentation for the Android MediaPlayer is not right about no invalid state for reset(). Below's what happen when I experienced it: In my PlayerActivity.java code, I set my MediaPlayer as static so that I can use it in my home activity: public class PlayerActivity extends Activity { .... public static MediaPlayer mp; @Override public void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     // Mediaplayer     if(mp == null) {         mp = new MediaPlayer();     }     .... } /**  * Function to play a song  * @param songIndex - index of song  * */ public void  playSong(int songIndex){     // Play song     try {         if(mUpdateTimeTask != null)             mHandler.removeCallbacks(mUpdateTimeTask);         mp.reset();             // the song path is get from internet     mp.setDataSource(songsList.get(songIndex).get("songPath"));     mp.prepareAsync();     } catch (IllegalArgumentException e) {         e.printStackTrace();     } catch (IllegalStateException e) {         e.printStackTrace();     } catch (IOException e) {         e.printStackTrace();     } } ... } In my home activity, I release the player before closing the app: public class TuoiTreAppActivity extends TabActivity {     ...     @Override     public void onDestroy(){         if(PlayerActivity.mp != null) {         PlayerActivity.mp.release();         }            super.onDestroy();     }     ... } So, when I launch the app for the first time and start playing a song. The reset() function runs well without error. But when I hit the back button to close the app and launch it for the second time, the IllegalStateException occurs when passing the reset() function. I also discovered the cause when debugging. As the first time running the app, the player is null so it is initialized in the onCreate() function of PlayerActivity.java. But the player is not release itself to null after the app was closed. So it is not initialized again when re-opening for the second time. That's the reason why IllegalStateException occurs when passing reset() function. So, to solve this problem, I have to set the player to null before closing the app: @Override public void onDestroy(){     if(PlayerActivity.mp != null) {         PlayerActivity.mp.release();         // Set the MediaPlayer to null to avoid IlLegalStateException              // when call mp.reset() after launching the app again         PlayerActivity.mp = null;     }     super.onDestroy(); }

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、付费专栏及课程。

余额充值