本文介绍了暂停时更新MediaPlayer的开始位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在直播音频,并希望具有暂停媒体播放器然后从当前直播位置开始的功能.默认的Pause()和Start()只是从停止的地方开始.关于如何保持当前位置更新以使Start()立即生效的任何想法?

I am streaming live audio and wish to have the functionality to pause the mediaplayer and then start from the current live position. The default Pause() and Start() just starts from where it was stopped. Any ideas on how to keep the current position updated so that Start() is instant?

推荐答案

您是否尝试调用seekTo()?

Did you try calling seekTo()?

通过使用 TimerTask ,但是我不知道我对此有何看法.

You could keep it pemanently updated by calling seekTo() every half a second or so with TimerTask, But i don't know how i feel about this.

编辑:如果每500毫秒调用一次seekTo(),为了使其继续前进,您可以使用currentPosition +自上次调用以来经过的时间(在这种情况下为500毫秒)来调用它.但是我不知道这是否是最好的方法.另一种方法是创建一个执行新的seekTo(currentPosition + timeEllapsed)的OnSeekCompleteListener.在这种情况下,您需要像这样计算timeEllapsed:currentSystemTime-systemTimeOnLastCall

If you call seekTo() every 500 milliseconds, in order to keep it moving forward you would call it with the currentPosition + time ellapsed since last call(500 milliseconds in this case). But again I don't know if this is the best aproach. Another way is to create a OnSeekCompleteListener that preforms a new seekTo(currentPosition + timeEllapsed). In this case you need to calculate timeEllapsed like so: currentSystemTime - systemTimeOnLastCall

来自 http://developer.android.com/reference/android/media/MediaPlayer.html :

可以通过调用seekTo(int)来调整播放位置.

The playback position can be adjusted with a call to seekTo(int).

尽管异步的seekTo(int)调用返回正确的方式,但是实际的搜索操作可能需要一段时间才能完成,尤其是对于音频/视频流.当实际搜索操作完成时,内部播放器引擎会调用提供的用户如果已通过setOnSeekCompleteListener(OnSeekCompleteListener)预先注册了OnSeekCompleteListener,则为OnSeekComplete.onSeekComplete().

Although the asynchronuous seekTo(int) call returns right way, the actual seek operation may take a while to finish, especially for audio/video being streamed. When the actual seek operation completes, the internal player engine calls a user supplied OnSeekComplete.onSeekComplete() if an OnSeekCompleteListener has been registered beforehand via setOnSeekCompleteListener(OnSeekCompleteListener).

请注意,还可以在其他状态下调用seekTo(int),例如Prepared,Paused和PlaybackCompleted状态.

Please note that seekTo(int) can also be called in the other states, such as Prepared, Paused and PlaybackCompleted state.

此外,可以通过调用getCurrentPosition()来检索实际的当前播放位置,这对于诸如音乐播放器之类的需要跟踪播放进度的应用程序很有帮助.

Furthermore, the actual current playback position can be retrieved with a call to getCurrentPosition(), which is helpful for applications such as a Music player that need to keep track of the playback progress.

这篇关于暂停时更新MediaPlayer的开始位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 13:31