MediaPlayer.getCurrentPosition IllegalStateException错误

简介:

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 }
复制代码


作者:kissazi2 
出处:http://www.cnblogs.com/kissazi2/ 
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

转载:http://www.cnblogs.com/kissazi2/archive/2012/09/06/2674340.html

目录
相关文章
|
18天前
|
C++
C++异常处理try和throw以及catch的使用
C++异常处理try和throw以及catch的使用
|
7月前
|
Java 程序员 API
异常(中)创建自定义异常,throw,throws关键字抛出异常
异常(中)创建自定义异常,throw,throws关键字抛出异常
67 0
|
4月前
|
Java 程序员 编译器
java异常处理,什么是Checked Exception和Unchecked Exception?
java异常处理,什么是Checked Exception和Unchecked Exception?
29 0
|
11月前
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState(已解决)
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState(已解决)
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionVie
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionVie
|
Android开发
RuntimeException: setAudioSource failed异常
RuntimeException: setAudioSource failed异常
157 0
|
XML 数据格式
使用SearchView报错java.lang.UnsupportedOperationException: Failed to resolve ...
SearchView是appcompat-v7中的一个组件,能够更快的实现一些基本的功能 但是在布局中使用android.support.v7.widget.SearchView,打开该页面会crash,查看错误是加载布局出问题了
733 0
|
Java 开发工具
addHeaderView()异常 —— setAdapter has already been called
addHeaderView()异常 —— setAdapter has already been called