gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:26:23 24 4
gpt4 key购买 nike

我有一个使用 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)

Note: -2147483648 is Integer MIN_VALUE

虽然 percent 应该是:

The percentage (0-100) of the content that has been buffered or played thus far

问题:

  1. 为什么在 onBufferingUpdate() percent 参数中传递了不正确的值?
  2. 如何解决?
  3. 是否有任何其他方法可以获取已缓冲的数据量?我的目标是显示一个进度条,让用户了解何时会缓冲足够的数据并开始播放广播。

谢谢!

最佳答案

我不确定为什么会这样,我遇到了同样的问题,但这段代码可能对你有帮助:

@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);

}

关于java - Android MediaPlayer.OnBufferingUpdateListener - 缓冲内容的百分比为负,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21925454/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com