android的mediaplayer在release后一定需要=null

本文探讨了在Android应用中复用MediaPlayer时遇到的IllegalStateException问题,并提供了解决方案。通过在应用退出时将MediaPlayer设置为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();


}
public class JavascriptBroadcastInterfaces { private MediaPlayer mediaPlayer; private String lastBroadcast; // 保存上一次收到的 broadcast @JavascriptInterface public void getBroadcast(String broadcast) { Log.i("Broadcast", "Received broadcast: " + broadcast); // 如果新值和上次相同,直接返回 if (broadcast != null && broadcast.equals(lastBroadcast)) { Log.i("Broadcast", "重复的 broadcast,跳过播放"); return; } // 更新 lastBroadcast 为新的值 lastBroadcast = broadcast; // 构造音频 URL String audioUrl = "http://192.168.3.150:816/mp3?s=" + broadcast; if (isUrlReachable(audioUrl, MainActivity3.this)) { // 开始播放音频 Log.e("Broadcast", "音频 URL :"+audioUrl); playAudio(audioUrl); } else { Log.e("Broadcast", "音频 URL 不可用"); } } private void playAudio(String audioUrl) { if (mediaPlayer != null) { mediaPlayer.release(); mediaPlayer = null; } mediaPlayer = new MediaPlayer(); try { mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.setDataSource(audioUrl); mediaPlayer.prepareAsync(); mediaPlayer.setOnPreparedListener(mp -> { Log.i("MediaPlayer", "音频准备成,开始播放"); mediaPlayer.start(); }); mediaPlayer.setOnCompletionListener(mp -> { Log.i("MediaPlayer", "播放成"); mediaPlayer.release(); mediaPlayer = null; }); mediaPlayer.setOnErrorListener((mp, what, extra) -> { Log.e("MediaPlayer", "播放错误,what: " + what + ", extra: " + extra); if (mediaPlayer != null) { mediaPlayer.release(); mediaPlayer = null; } return true; }); } catch (Exception e) { Log.e("MediaPlayer", "设置数据源失败", e); if (mediaPlayer != null) { mediaPlayer.release(); mediaPlayer = null; } } } }这段代码播放音频地址在浏览器播放是正常的,在代码里播放是乱码
最新发布
07-25
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值