Android - 如何判断 MediaPlayer 何时正在缓冲
我在这里必须遗漏一些明显的东西,但我似乎找不到任何东西可以让我确定 MediaPlayer 何时缓冲音频。我正在流式传输互联网音频,并且想要显示缓冲指示器,但我尝试过的任何操作都不允许我知道 MediaPlayer 何时中断音频进行缓冲,因此我无法正确显示缓冲指示器。有什么线索吗?
I've got to be missing something obvious here, but I can't seem to find anything to allow me to determine when MediaPlayer is buffering audio. I'm streaming internet audio and I want to display a buffering indicator, but nothing I've tried allows me to know when MediaPlayer interrupts the audio to buffer, so I can't properly display a buffering indicator. Any clues?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如下所示(API 级别 ≥ 9):
注意:Android 中存在一个已知错误。播放 HLS 流时,它永远不会调用 OnInfoListener 或 OnBuffering。检查此链接OnInfoListener bug
Like below (API level ≥ 9):
NOTE : There is a known bug in Android. When playing HLS stream it's just never calls OnInfoListener or OnBuffering. check this link OnInfoListener bug
好吧,我现在感觉有点平反了。我检查了 Pandora 应用程序,它没有显示缓冲指示器。当音乐因缓冲而中断时,它只是坐在那里,就好像什么也没发生一样,并且 UI 看起来仍在播放。所以我得出的结论是,如果您使用 MediaPlayer,则无法确定曲目是否暂时暂停进行缓冲。
但是,我确实注意到有几个 MediaPlayer 常量可以使用:
MEDIA_INFO_BUFFERING_START和MEDIA_INFO_BUFFERING_END。但它们仅在 API 级别 9+ 中可用,并且文档没有提及任何有关它们的信息。我假设它们可以与 OnInfoListener 一起使用。
我很失望,但至少我现在可以停止原地踏步,转向其他事情了。
Ok, I feel a little vindicated now. I checked out the Pandora app and it doesn't display a buffering indicator. When music is interrupted for buffering, it just sits there as if nothing happened and the UI looks like it's still playing. So I've come to the conclusion that if you're using MediaPlayer, it's just not possible to determine if the track is temporarily paused for buffering.
However, I did notice that there are a couple MediaPlayer constants that could be of use:
MEDIA_INFO_BUFFERING_START and MEDIA_INFO_BUFFERING_END. But they're only available in API level 9+, and the docs don't say anything about them. I'm assuming they can be used with an OnInfoListener.
I'm disappointed, but at least I can stop spinning my wheels now and move on to something else.
@Daniel,根据您对 @JRL 答案的评论,您可能可以通过旋转线程并等待超时来实现此目的。
像 DetectBufferTimeout.java (未经测试)这样的东西会很好。
然而,我确实同意旋转这个单独的线程有点麻烦。也许
OnBufferingUpdateListener
可以保证调用onBufferingUpdate()
的频率,无论缓冲进度是否发生变化,这样我们就可以检测是否正在获取一遍又一遍相同的值。@Daniel, per your comment on @JRL's answer, you could probably get this working by spinning up a thread and waiting for a timeout.
Something like DetectBufferTimeout.java (untested) would do nicely.
I do, however, agree that spinning up this separate thread is a bit of a hack. Perhaps
OnBufferingUpdateListener
could make a guarantee as to how often it callsonBufferingUpdate()
regardless of whether a change in the buffering progress has occurred so we can detect if we're getting the same value over and over.注册一个 OnBufferingUpdate 监听器。
Register an OnBufferingUpdate listener.
您可以使用一个线程来检查 MediaPlayer 的当前位置。如果位置没有变化,则可以断定媒体处于缓冲状态。这是我编写的完整教程: http://www.ottodroid.net/?p=260< /a>
You can use a thread that checks the current position of the MediaPlayer. If the position doesnt change, you can conclude that the media is in buffering state. Here is the complete tutorial that i wrote: http://www.ottodroid.net/?p=260
就像@JRL所说,注册一个 OnBufferUpdateListener 但将其注册到 OnPreparedListener 中的 MediaPlayer 对象上,这样无论何时音乐缓冲它都会总是表明。因为当媒体播放器缓冲时始终会调用此侦听器。像这样:
Just like @JRL said, register a
OnBufferUpdateListener
but register it on theMediaPlayer
object inOnPreparedListener
, that way anytime the music is buffering it'll always indicate. as this listener is always called when mediaplayer is buffering. like so: