Android MediaPlayer – 位置在缓冲时跳过

在某些设备上播放音乐时,我遇到了一个奇怪的行为。 当audio第一次开始播放时,位置跳转并播放,直到缓冲完全结束。

我从Tutorials Point的示例代码开始。 我在OnCreate()创buildMediaPlayer,如下所示:

 // 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); } 

我得到的日志输出显示了媒体播放器仍在缓冲时位置如何跳跃。 我已经testing了几个设备,这只发生在一个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 

Solutions Collecting From Web of "Android MediaPlayer – 位置在缓冲时跳过"

在开发者文档中有与此相关的注释:

reset()被调用后,新构造的MediaPlayer对象和MediaPlayer对象之间存在细微但重要的区别。 对于这两种情况,在空闲状态下调用诸如getCurrentPosition()等方法都是编程错误

它还增加了:

需要注意的是,“准备”状态是一种暂时状态,在MediaPlayer对象处于“准备”状态时调用具有副作用的任何方法的行为是未定义的。

onPreparedcallback之前,调用getCurrentPosition()似乎并不安全。 解决方法可能是在caching时暂停播放。