Android MediaPlayer.OnBufferingUpdateListener - 缓冲内容的百分比为负数

时间:2014-02-21 04:43:00

标签: java android android-service android-mediaplayer buffering

我有一项使用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)   到目前为止

问题

  1. 为什么在onBufferingUpdate() percent参数中传递了错误的值?
  2. 如何解决这个问题?
  3. 有没有其他方法可以获得缓冲的数据量?我的目标是显示一个进度条,让用户知道何时可以缓冲足够的数据并开始播放无线电。
  4. 谢谢!

3 个答案:

答案 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);
}

OnCreateViewOnCreate

的上方
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;