获取mediaPlayer.getCurrentPosition时会随机出现java.lang.illegalStateException [英] java.lang.illegalStateException randomly occurs when getting mediaPlayer.getCurrentPosition

查看:1106
本文介绍了获取mediaPlayer.getCurrentPosition时会随机出现java.lang.illegalStateException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这段代码完美地播放了精美的歌曲,但是有些时间随机转到下一首歌曲会导致应用程序崩溃。随机发生

This code works fine song play perfectly but some time randomly going to next song manually crashes application. Happens randomly

    updateSeekBar = new Thread() {
        @Override
        public void run() {
            int runtime = mediaPlayer.getDuration();
            int currentPosition = 0;
            while (currentPosition < runtime) {
                try {
                    sleep(500);
                    currentPosition = mediaPlayer.getCurrentPosition();//This is where the app crash                        
                    if (seekBar != null) {
                        seekBar.setProgress(currentPosition);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }
    };

崩溃报告

06-10 22:08:53.160 15351-15560/skydeveloper.me.musicx2 E/AndroidRuntime: FATAL EXCEPTION: Thread-6875
                                                                     Process: skydeveloper.me.musicx2, PID: 15351
                                                                     java.lang.IllegalStateException
                                                                         at android.media.MediaPlayer.getCurrentPosition(Native Method)
                                                                         at skydeveloper.me.musicx2.Player$1.run(Player.java:104)


推荐答案

实际上如果媒体播放器处于错误状态会出现此问题。

Actually this problem occurs if Media player is in wrong state.

如果您的MediaPlayer位于已初始化状态,则无法调用 start()。因此,您必须等到您的MediaPlayer处于准备状态位置。
您只能在准备,开始和暂停状态下搜索媒体播放器。

If your MediaPlayer is in the Initialized-State, you cannot call start(). So you have to wait till your MediaPlayer is in the Prepared-State position. You can only seek the Mediaplayer in Prepared, Started and Paused State.

有关详情,请查看:媒体播放器状态图

您也可以拨打当前位置的第二个电话。由于这是随机问题,然后绕过并再次调用该方法可以节省您的费用。

You can also make second call for current position. As it is random issue then bypassing and again calling the method can save you.

mediaPlayer.reset();

try {
   .......
   .......
   currentPosition = mediaPlayer.getCurrentPosition();
   ......
} catch (IllegalStateException e) {
   mediaPlayer.reset();
   currentPosition = mediaPlayer.getCurrentPosition();
}

mediaPlayer.prepareAsync();

资源链接:
这是什么错误在MediaPlayer.getCurrentPosition上的java.lang.IllegalStateException

您可以绕过异常并使用它进行第二次通话。希望它可以帮到你。

You can bypass the exception and make a second call by using it. Hope it may help you.

try {
....
currentPosition = mediaPlayer.getCurrentPosition();
....
} catch (final Exception e) {
    e.printStackTrace();
    if (e instanceof IllegalStateException) { // bypass IllegalStateException
        .......
        // You can again call the method and make a counter for deadlock situation or implement your own code according to your situation
        if (retry) {
               mediaPlayer.reset();
               currentPosition = mediaPlayer.getCurrentPosition();
        } else {
            throw e;
        }
    }
}



UPDATE2:



这将尝试3次获得当前位置。如果没有找到,那么它将给出例外。

UPDATE2:

This will try 3 times to get current position. If not found, then it will give exception.

 try {
     ....
     currentPosition = mediaPlayer.getCurrentPosition();
     ....
     } catch (final Exception e) {
         e.printStackTrace();
         if (e instanceof IllegalStateException) { // bypass IllegalStateException
             .......
             // You can again call the method and make a counter for deadlock situation or implement your own code according to your situation
             boolean checkAgain = true;
             int counter = 0;
                 for(int i = 0; i < 2; i++){
                     if (checkAgain) {
                         mediaPlayer.reset();
                         currentPosition = mediaPlayer.getCurrentPosition();
                         if(currentPosition > 0) {
                             checkAgain = false;
                             counter++;
                         }
                     } else {
                         if(counter == 0){
                             throw e;
                         }
                     }
                 }


         }
     }



UPDATE4:



UPDATE4:


  1. 一个 IllegalStateException prepare() prepareAsync()在任何其他状态下调用
    ,则抛出>。

  2. 当处于准备状态时,可以通过调用
    对应的set方法来调整音频/音量,
    screenOnWhilePlaying,循环等属性。

  1. An IllegalStateException is thrown if prepare() or prepareAsync() is called in any other state.
  2. While in the Prepared state, properties such as audio/sound volume, screenOnWhilePlaying, looping can be adjusted by invoking the corresponding set methods.

要开始播放,必须调用 start()。在 start()成功返回
后,MediaPlayer对象处于Started状态。
可以调用isPlaying()来测试MediaPlayer对象在启动状态下是否为

To start the playback, start() must be called. After start() returns successfully, the MediaPlayer object is in the Started state. isPlaying() can be called to test whether the MediaPlayer object is in the Started state.

当处于已启动状态时,内部播放器引擎调用用户
提供 OnBufferingUpdateListener.onBufferingUpdate()回调
方法如果事先已注册OnBufferingUpdateListener
通过 setOnBufferingUpdateListener(OnBufferingUpdateListener)。这个
回调允许应用程序在流式传输音频/视频时跟踪缓冲状态
。调用 start()对已经处于已启动状态的
MediaPlayer对象没有影响。

While in the Started state, the internal player engine calls a user supplied OnBufferingUpdateListener.onBufferingUpdate() callback method if a OnBufferingUpdateListener has been registered beforehand via setOnBufferingUpdateListener(OnBufferingUpdateListener). This callback allows applications to keep track of the buffering status while streaming audio/video. Calling start() has not effect on a MediaPlayer object that is already in the Started state.

所以应该检查 isPlaying()方法。因此代码如下所示:

So isPlaying() method should be checked. So code will be look like below:

 try {
     ....
     currentPosition = mediaPlayer.getCurrentPosition();
     ....
     } catch (final Exception e) {
         e.printStackTrace();
         if (e instanceof IllegalStateException) { // bypass IllegalStateException
             .......
             // You can again call the method and make a counter for deadlock situation or implement your own code according to your situation
             boolean checkAgain = true;
             int counter = 0;
                 for(int i = 0; i < 2; i++){
                     if (checkAgain) {
                         mediaPlayer.reset();
                         if(mediaPlayer != null & mediaPlayer.isPLaying()) {
                            currentPosition = mediaPlayer.getCurrentPosition();
                         } else {
                            currentPosition = 0; 
                         }
                         if(currentPosition > 0) {
                             checkAgain = false;
                             counter++;
                         }
                     } else {
                         if(counter == 0){
                             throw e;
                         }
                     }
                 }


         }
     }

这篇关于获取mediaPlayer.getCurrentPosition时会随机出现java.lang.illegalStateException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆