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

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

问题描述

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

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进行开发。如果它可以在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.

与往常一样,您可以在文档中阅读更多内容: AVPlayerItem AVPlayer

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天全站免登陆