我有一项使用MediaPlayer
来传输互联网广播的服务:
public class MediaPlayerService extends Service implements
MediaPlayer.OnPreparedListener,
MediaPlayer.OnBufferingUpdateListener {
private MediaPlayer mMediaPlayer;
private WifiLock mWifiLock;
public int onStartCommand(Intent intent, int flags, int startId) {
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setOnBufferingUpdateListener(this);
mMediaPlayer.setOnPreparedListener(this);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setDataSource(MY_URL);
// Acquire CPU lock and wi-fi lock
mMediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
mWifiLock = ((WifiManager) getSystemService(Context.WIFI_SERVICE))
.createWifiLock(WifiManager.WIFI_MODE_FULL, "Media Player Wi-Fi Lock");
mWifiLock.acquire();
mMediaPlayer.prepareAsync();
return START_STICKY;
}
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
mMediaPlayer.start();
}
@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
Log.d("Buffered " + percent);
}
}
除onBufferingUpdate(...)
方法外,一切都很有效。它在日志中打印出来:
Buffered -819200000
Buffered -819200000
Buffered -1228800000
Buffered -1228800000
Buffered -1228800000
Buffered -1228800000
Buffered -1638400000
Buffered -1638400000
Buffered -1638400000
Buffered -1638400000
Buffered -2048000000
Buffered -2048000000
Buffered -2048000000
Buffered -2048000000
Buffered -2147483648 (repeated further on)
注意:-2147483648是整数MIN_VALUE
虽然percent
应该是:
已缓冲或播放的内容的百分比(0-100) 到目前为止
问题:
onBufferingUpdate()
percent
参数中传递了错误的值? 谢谢!
答案 0 :(得分:0)
我不确定为什么会发生这种情况,我遇到了同样的问题,但是此代码可能对您有所帮助:
@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
// TODO Auto-generated method stub
if (percent < 0 || percent > 100) {
//System.out.println("Doing math: (" + (Math.abs(percent)-1)*100.0 + " / " +Integer.MAX_VALUE+ ")" );
percent = (int) Math.round((((Math.abs(percent)-1)*100.0/Integer.MAX_VALUE)));
}
progressBar.setProgress(percent);
Log.i("Buffering! " , " " +percent );
System.out.println("Buffering: " +percent);
}
答案 1 :(得分:0)
public void onBufferingUpdate(MediaPlayer mp, int percent)
{
double ratio = percent / 100.0;
bufferingLevel = (int)(mp.getDuration() * ratio);
seekBar.setSecondaryProgress(bufferingLevel);
}
在OnCreateView
或OnCreate
seekBar.setMax(GET_TOTAL_DURATION_OF_SONG);
答案 2 :(得分:0)
问题出在Android Player Driver中,point是有符号/无符号值。这是相关的issue。
要在JVM级别解决它,您可以使用以下代码:
percent &= 0xFF;
// media broadcasts may return values more than 100
if (percent > 100)
percent = 0;