可以用Alamofire播放同步下载的视频文件吗?
Is this possible to play Video File synchronous downloading with Almofire?
我尝试使用 Alamofire
下载文件,并且取得了进展。我现在要做的是从视频 URL.
构建一种本地流
Alamofire 将下载的文件存储在目标位置。
let destination = Alamofire.Request.suggestedDownloadDestination(directory: .DocumentDirectory, domain: .LocalDomainMask)
这个目的地在我的情况下无效。例如
if totalBytesRead >= 51831 {
let player = AVPlayer(URL: NSURL(fileURLWithPath: "\(destination)"))
let playerController = AVPlayerViewController()
playerController.player = player
self.presentViewController(playerController, animated: true) {
player.play()
}
}
还有其他方法吗?或者我可以将下载的字节存储到 array/buffer ?
谢谢
您不需要 Alamofire 来流式传输您的视频内容。
您实际上可以利用现有的 Apple classes,例如:
使用这 3 个 classes,您可以创建自己的 class,例如 VideoPlayer,将使用它们进行流式传输。
这是构造函数的代码片段,可帮助您入门:
playerItem = AVPlayerItem(URL: videoURL)
player = AVPlayer(playerItem: self.playerItem!)
playerLayer = AVPlayerLayer(player: self.player)
playerLayer?.frame = newFrame
playerLayer?.videoGravity = AVLayerVideoGravityResizeAspect
现在,您可以在另一个视图控制器中使用这个 class:
self.containerView.layer.addSublayer(self.videoPlayer.playerLayer)
这将添加您创建的视频播放器class,现在您只需播放视频:
// Of type AVPlayer
player.play()
这是比较简单的部分,如果您愿意,您将不得不实施自己的缓冲区和速率,当然还要实施一个观察器来观察速率和缓冲区:
// Observe the Rate of the player. when the video playing or paused
player.addObserver(theSelf, forKeyPath: "rate", options: .New, context: nil)
// Observe when the Video player Buffer is empty
playerItem.addObserver(theSelf, forKeyPath: "playbackBufferEmpty", options: .New, context: nil)
// Observe the loaded Buffer of the video
playerItem.addObserver(theSelf, forKeyPath: "loadedTimeRanges", options: NSKeyValueObservingOptions.New, context: nil)
此外,不要忘记在用完后将其删除。
编辑:
如前所述,如果您需要在 运行 时间内切换到更好的质量,您首先需要观察用户的网速。解决此问题后,您现在需要流式传输高质量视频,例如在当前层的顶部添加另一个 VideoPlayer 层,但只有在新的高质量视频缓冲区至少有 5 秒左右时才这样做,因此用户不会注意到变化。
祝你好运。
我尝试使用 Alamofire
下载文件,并且取得了进展。我现在要做的是从视频 URL.
Alamofire 将下载的文件存储在目标位置。
let destination = Alamofire.Request.suggestedDownloadDestination(directory: .DocumentDirectory, domain: .LocalDomainMask)
这个目的地在我的情况下无效。例如
if totalBytesRead >= 51831 {
let player = AVPlayer(URL: NSURL(fileURLWithPath: "\(destination)"))
let playerController = AVPlayerViewController()
playerController.player = player
self.presentViewController(playerController, animated: true) {
player.play()
}
}
还有其他方法吗?或者我可以将下载的字节存储到 array/buffer ?
谢谢
您不需要 Alamofire 来流式传输您的视频内容。
您实际上可以利用现有的 Apple classes,例如:
使用这 3 个 classes,您可以创建自己的 class,例如 VideoPlayer,将使用它们进行流式传输。
这是构造函数的代码片段,可帮助您入门:
playerItem = AVPlayerItem(URL: videoURL)
player = AVPlayer(playerItem: self.playerItem!)
playerLayer = AVPlayerLayer(player: self.player)
playerLayer?.frame = newFrame
playerLayer?.videoGravity = AVLayerVideoGravityResizeAspect
现在,您可以在另一个视图控制器中使用这个 class:
self.containerView.layer.addSublayer(self.videoPlayer.playerLayer)
这将添加您创建的视频播放器class,现在您只需播放视频:
// Of type AVPlayer
player.play()
这是比较简单的部分,如果您愿意,您将不得不实施自己的缓冲区和速率,当然还要实施一个观察器来观察速率和缓冲区:
// Observe the Rate of the player. when the video playing or paused
player.addObserver(theSelf, forKeyPath: "rate", options: .New, context: nil)
// Observe when the Video player Buffer is empty
playerItem.addObserver(theSelf, forKeyPath: "playbackBufferEmpty", options: .New, context: nil)
// Observe the loaded Buffer of the video
playerItem.addObserver(theSelf, forKeyPath: "loadedTimeRanges", options: NSKeyValueObservingOptions.New, context: nil)
此外,不要忘记在用完后将其删除。
编辑:
如前所述,如果您需要在 运行 时间内切换到更好的质量,您首先需要观察用户的网速。解决此问题后,您现在需要流式传输高质量视频,例如在当前层的顶部添加另一个 VideoPlayer 层,但只有在新的高质量视频缓冲区至少有 5 秒左右时才这样做,因此用户不会注意到变化。
祝你好运。