前进时,AVPlayer 中的 SeekToTime 停止播放流音频 [英] SeekToTime in AVPlayer stops playing streaming audio, when forward

查看:35
本文介绍了前进时,AVPlayer 中的 SeekToTime 停止播放流音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 AVPlayer 流式传输音频.它运行良好.但是现在我需要制作一个滑块来向前和向后移动音频,就像典型的音乐播放器一样.我使用了 seekToTime 函数,它适用于本地音频文件.但是,当我从 web url 流式传输歌曲时,当我以大范围向前滑动时,该功能会停止音频.

I am streaming audio by using AVPlayer. It is working well. But Now i need to make a slider to move audio forward and backward, like typical music player. I used the function seekToTime which works great with local audio file. But when i stream song from web url, the function stops audio when I forward slider with a large range.

我的猜测是歌曲正在下载,如果我将滑块向前移动大范围,那么可能是系统在那段时间没有下载歌曲数据包,所以它停止播放.

My guess is that the song is being downloaded and if i move slider forward with large range then may be system has not downloaded the song data packets for that time, so it halts player.

假设我只是点击了一个按钮来播放歌曲,但现在我在 0 到 100 秒立即移动滑块.在这种情况下,系统不工作并停止播放器.由于当时缺少数据包.

Lets say i just hit a button to stream song but now i move slider immediately at 0 to 100th second. In this case system is not working and stops the player. Due to lack of data packets for that time.

有没有人知道如何克服这个问题,或者有没有其他方法.我正在使用 SWIFT 进行开发.如果可以快速运行,我也可以使用任何库.这是我的功能:

Has anyone know how to overcome this problem or is there any other approach. I am using SWIFT for development. I am up to use any library as well if it would work in swift. This is my function:

func forward () {
    timeGap = slider.value
    let preferredTimeScale : Int32 = 1
    let targetTime : CMTime = CMTimeMake(timeGap, preferredTimeScale)

    player.seekToTime(targetTime, toleranceBefore: kCMTimeZero, toleranceAfter: kCMTimeZero)

}

提前致谢.

推荐答案

这个答案很大程度上基于这个答案:https://stackoverflow.com/a/7195954/765298.任何和所有功劳都应该放在那里,因为这只是对 swift 的翻译.

This answer is heavily based on this one : https://stackoverflow.com/a/7195954/765298 . Any and all credit should go there as this is merely a translation to swift.

您正确地假设,当向前寻找尚未缓冲但播放器停止的部分时.问题是它仍然缓冲数据,但在准备好时不会自动启动.因此,将链接的答案改写为 swift :

You are right to assume that when seeking far forward to a part that has not been buffered yet the player stops. The thing is it still buffers the data, but doesn't start automatically when it is ready. So, to rephrase the linked answer to swift :

设置观察者:

player.currentItem?.addObserver(self, forKeyPath: "playbackBufferEmpty", options: .New, context: nil)
player.currentItem?.addObserver(self, forKeyPath: "playbackLikelyToKeepUp", options: .New, context: nil)

注意,为了能够观察值,传入的self需要继承自NSObject.

Note, that in order to be able to observe values, the passed self needs to inherit from NSObject.

并处理它们:

override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {

    guard keyPath != nil else { // a safety precaution
        super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
        return
    }

    switch keyPath! {
        case "playbackBufferEmpty" :
            if player.currentItem!.playbackBufferEmpty {
                print("no buffer")
                // do something here to inform the user that the file is buffering
            }

        case "playbackLikelyToKeepUp" :
            if player.currentItem!.playbackLikelyToKeepUp {
                self.player.play()
                // remove the buffering inidcator if you added it
            }
    }
}

您还可以从当前播放的 AVPlayerItem 中获取有关可用时间范围的信息(如果您自己没有创建,可以通过 player.currentItem 访问它).这使您能够向用户指示文件的哪些部分已准备就绪.

You can also get information about avaiable time ranges from the currently playing AVPlayerItem (you can acces it via player.currentItem if you haven't created it yourself). This enables you to indicate to user which parts of the file are ready to go.

一如既往,您可以在文档中阅读更多内容:AVPlayerItemAVPlayer

As always you can read some more in the docs : AVPlayerItem and AVPlayer

要阅读有关键值观察 (KVO) 的更多信息:这里

To read more about key-value observing (KVO) : here

这篇关于前进时,AVPlayer 中的 SeekToTime 停止播放流音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆