MediaPlayer.getCurrentPosition IllegalStateException错误
发布日期:2021-09-08 15:09:11 浏览次数:27 分类:技术文章

本文共 1864 字,大约阅读时间需要 6 分钟。

09-06 20:13:48.298: E/AndroidRuntime(11463): FATAL EXCEPTION: Thread-11

09-06 20:13:48.298: E/AndroidRuntime(11463): java.lang.IllegalStateException
09-06 20:13:48.298: E/AndroidRuntime(11463): at android.media.MediaPlayer.getCurrentPosition(Native Method)
09-06 20:13:48.298: E/AndroidRuntime(11463): at com.leranxinsheng.activity.MusicService.run(MusicService.java:236)
09-06 20:13:48.298: E/AndroidRuntime(11463): at java.lang.Thread.run(Thread.java:1019)

 

当从播放的界面跳到其他界面的时候,总是爆出MediaPlayer.getCurrentPosition IllegalStateException的错误。后来想了一下,是因为播放器所在的Activity已经进入OnStop状态,再让后台调整音乐进度条的服务获取进度条(MediaPlayer.getCurrentPosition)肯定不合法。

 

 

原代码如下

1     public void run() { 2         // TODO Auto-generated method stub 3         while (isRun) { 4             try { 5                 Thread.sleep(200); 6             } catch (InterruptedException e) { 7                 // TODO Auto-generated catch block 8                 e.printStackTrace(); 9             }10 //这段是增加判断后的代码/11             if (null != player) {12                  if (!player.isPlaying()) {//如果不在播放状态,则停止更新 //播放器进度条,防止界面报错13                  Log.d(strTag, "播放器停止播放,跳过获取位置");14                  break;15                  }16 ///               17                 int position = player.getCurrentPosition();18                 int total = player.getDuration();19                 Intent intent = new Intent("com.leranxinsheng.progress");20                 intent.putExtra("position", position);21                 intent.putExtra("total", total);22                 sendBroadcast(intent);23             }24             25             if (null != player) {26                 if (player.isPlaying()) {27                     playing = true;28                 } else {29                     Log.d(strTag, "播放器停止播放,跳过第一个if");30                     playing = false;31                 }32             }33         }34     }

转载地址:https://blog.csdn.net/weixin_34326429/article/details/90154493 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:[ucgui] 对话框5——鼠标位置和移动窗口
下一篇:准备精读开源的.NET代码,希望能坚持

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2024年04月15日 21时41分07秒