这段代码可以完美地播放精美的歌曲,但有些时间随机转到下一首歌会手动崩溃应用程序.随机发生
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();
}
}
}
};
Run Code Online (Sandbox Code Playgroud)
崩溃报告
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)
Run Code Online (Sandbox Code Playgroud)
实际上,如果媒体播放器处于错误状态,则会出现此问题。
如果您的 MediaPlayer 在 中Initialized-State,则无法调用start()。所以你必须等到你的 MediaPlayerPrepared-State就位。
您只能寻找处于准备、启动和暂停状态的媒体播放器。
有关更多详细信息,您可以查看:媒体播放器状态图
您还可以对当前位置进行第二次调用。由于这是随机问题,因此绕过并再次调用该方法可以节省您的时间。
mediaPlayer.reset();
try {
.......
.......
currentPosition = mediaPlayer.getCurrentPosition();
......
} catch (IllegalStateException e) {
mediaPlayer.reset();
currentPosition = mediaPlayer.getCurrentPosition();
}
mediaPlayer.prepareAsync();
Run Code Online (Sandbox Code Playgroud)
资源链接: MediaPlayer.getCurrentPosition 处的 java.lang.IllegalStateException 是什么错误
您可以绕过异常并使用它进行第二次调用。希望它可以帮助你。
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
这将尝试 3 次以获取当前位置。如果没有找到,那么它会给出异常。
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;
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
IllegalStateException如果prepare()或prepareAsync()在任何其他状态下被调用,则抛出An 。在 Prepared 状态下,可以通过调用相应的 set 方法来调整音频/音量、screenOnWhilePlaying、looping 等属性。
要开始播放,start()必须调用。后start()成功返回后,MediaPlayer对象是处于启动状态。
可以调用 isPlaying() 来测试 MediaPlayer 对象是否处于 Started 状态。
在 Started 状态下,OnBufferingUpdateListener.onBufferingUpdate()如果预先通过 注册了 OnBufferingUpdateListener ,则内部播放器引擎会调用用户提供的回调方法setOnBufferingUpdateListener(OnBufferingUpdateListener)。此回调允许应用程序在流式传输音频/视频时跟踪缓冲状态。调用start()对已经处于 Started 状态的 MediaPlayer 对象没有影响。
所以isPlaying()应该检查方法。所以代码将如下所示:
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;
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
当您尝试在播放器停止或释放时获取媒体播放器的位置时,这可能是可能的,因此请应用以下条件,
if(mediaPlayer != null & mediaPlayer.isPLaying())
currentPosition = mediaPlayer.getCurrentPosition();
else
currentPosition = 0;
Run Code Online (Sandbox Code Playgroud)
尝试下面的代码,
handler.postDelayed(new Runnable(){
@Override
public void run() {
int runtime = mediaPlayer.getDuration();
int currentPosition = 0;
while (currentPosition < runtime) {
try {
if(mediaPlayer != null & mediaPlayer.isPLaying())
currentPosition = mediaPlayer.getCurrentPosition();
else
currentPosition = 0;
if (seekBar != null) {
seekBar.setProgress(currentPosition);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, 500);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4042 次 |
| 最近记录: |