在某些设备上播放音乐时,我遇到了一种奇怪的行为。当音频首次开始播放时,位置会跳跃并且不稳定地播放,直到缓冲完全完成。
我从sample code from Tutorials Point开始。
我正在OnCreate()
中创建MediaPlayer:
// Create Media Player
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnBufferingUpdateListener(this);
mediaPlayer.reset();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
prepareMediaPlayer();
prepareMediaPlayer
设置如下数据:
private void prepareMediaPlayer() {
try {
mediaPlayer.setDataSource(myurl);
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
Toast.makeText(getApplicationContext(),
"Playing sound",Toast.LENGTH_SHORT).show();
mediaPlayer.start();
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
和onBufferingUpdate
打印出缓冲区进度:
@Override
public void onBufferingUpdate(MediaPlayer mediaPlayer, int bufferingProgress) {
Log.v(TAG, "onBufferingUpdate() "+bufferingProgress +
"\ncurrentposition: "+mediaPlayer.getCurrentPosition());
seekbar.setSecondaryProgress(bufferingProgress);
}
我得到的日志输出显示了当媒体播放器仍在缓冲时位置如何跳跃。我已经在一些设备上进行了测试,这只发生在Moto G上。这是固件中的错误还是我做错了什么?
08-29 12:50:17.934 277/com.test.mediaplayer V/audio-player: onBufferingUpdate() 4
currentposition: 0
08-29 12:50:17.934 277/com.test.mediaplayer D/MediaPlayer: setSubtitleAnchor in MediaPlayer
08-29 12:50:17.976 277/com.test.mediaplayer V/audio-player: onBufferingUpdate() 4
currentposition: 0
08-29 12:50:18.014 27927-27945/com.test.mediaplayer V/RenderScript: 0xb7912ee8 Launching thread(s), CPUs 4
08-29 12:50:21.414 277/com.test.mediaplayer V/audio-player: onBufferingUpdate() 8
currentposition: 7
08-29 12:50:34.930 277/com.test.mediaplayer V/audio-player: onBufferingUpdate() 11
currentposition: 9
08-29 12:50:50.085 277/com.test.mediaplayer V/audio-player: onBufferingUpdate() 15
currentposition: 4
08-29 12:51:32.991 277/com.test.mediaplayer V/audio-player: onBufferingUpdate() 19
currentposition: 2
08-29 12:53:25.036 277/com.test.mediaplayer V/audio-player: onBufferingUpdate() 22
currentposition: 5
08-29 12:53:51.976 277/com.test.mediaplayer V/audio-player: onBufferingUpdate() 26
currentposition: 1
08-29 12:56:26.004 27927-27946/com.test.mediaplayer I/MediaHTTPConnection: proxyName: 0.0.0.0 0
08-29 12:56:34.651 277/com.test.mediaplayer V/audio-player: onBufferingUpdate() 30
currentposition: 10
08-29 12:56:35.862 277/com.test.mediaplayer V/audio-player: onBufferingUpdate() 33
currentposition: 1102
08-29 12:56:38.211 277/com.test.mediaplayer V/audio-player: onBufferingUpdate() 37
currentposition: 4
08-29 12:56:40.097 277/com.test.mediaplayer V/audio-player: onBufferingUpdate() 41
currentposition: 9
08-29 12:56:43.034 277/com.test.mediaplayer V/audio-player: onBufferingUpdate() 44
currentposition: 9
08-29 12:56:45.130 277/com.test.mediaplayer V/audio-player: onBufferingUpdate() 48
currentposition: 3
08-29 12:56:49.841 277/com.test.mediaplayer V/audio-player: onBufferingUpdate() 52
currentposition: 8
08-29 12:56:52.500 277/com.test.mediaplayer V/audio-player: onBufferingUpdate() 55
currentposition: 8
08-29 12:56:55.748 277/com.test.mediaplayer V/audio-player: onBufferingUpdate() 63
currentposition: 7
08-29 12:56:57.080 277/com.test.mediaplayer V/audio-player: onBufferingUpdate() 74
currentposition: 1223
08-29 12:56:59.072 277/com.test.mediaplayer V/audio-player: onBufferingUpdate() 81
currentposition: 979
08-29 12:57:00.073 277/com.test.mediaplayer V/audio-player: onBufferingUpdate() 81
currentposition: 1979
08-29 12:57:03.596 277/com.test.mediaplayer V/audio-player: onBufferingUpdate() 88
currentposition: 5
08-29 12:57:05.862 277/com.test.mediaplayer V/audio-player: onBufferingUpdate() 92
currentposition: 4
08-29 12:57:08.291 277/com.test.mediaplayer V/audio-player: onBufferingUpdate() 95
currentposition: 8
08-29 12:57:13.903 277/com.test.mediaplayer V/audio-player: onBufferingUpdate() 99
currentposition: 9
08-29 12:57:14.904 277/com.test.mediaplayer V/audio-player: onBufferingUpdate() 100
currentposition: 668
08-29 12:57:34.261 277/com.test.mediaplayer V/audio-player: onBufferingUpdate() 100
currentposition: 224068
答案 0 :(得分:2)
developer docs中有与此相关的注释:
在调用reset()之后,新构造的MediaPlayer对象与MediaPlayer对象之间存在微妙但重要的区别。对于两种情况,在空闲状态下调用getCurrentPosition()等方法都是编程错误
它还补充说:
重要的是要注意,准备状态是一种瞬态,并且在MediaPlayer对象处于准备状态时调用任何具有副作用的方法的行为是未定义的。
在getCurrentPosition()
回调之后,您似乎不安全地致电onPrepared
。解决方案可能是在缓冲时暂停播放。