来自DB的iOS-视频流非常慢。如何让它加载更快?

时间:2017-03-12 10:04:13

标签: ios video video-streaming avplayer

我正在尝试使用Swift从服务器播放视频。

我已导入AVKit和AVFoundation框架,这是我的代码:

import UIKit
import AVKit
import AVFoundation


class SongViewController: UIViewController, SendInfoDelegate {

let keyPathLoadedTimeRanges = "currentItem.loadedTimeRanges"
var songInfo : Dictionary<String, Any>!
var player : AVPlayer!
var previousVC : UIViewController!
let movie = AVPlayerViewController()

.....

override func viewWillDisappear(_ animated: Bool) {
    player.pause()
    movie.player = nil
    movie.removeFromParentViewController()
    self.removeFromParentViewController()
}

override func viewDidAppear(_ animated: Bool) {
    view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
}

...

 func loadVideo (videoURL: String) {

    //Load video in Avplayer
    let movieurl : URL = URL(string: videoURL)!

    movie.view.frame = videoContainerView.bounds

    player = AVPlayer(url: movieurl)
    movie.player = player
    self.addChildViewController(movie)
    videoContainerView.addSubview(movie.view)

    player.play()

    player.addObserver(self, forKeyPath: keyPathLoadedTimeRanges, options: .new, context: nil)

    controllsContainerView.frame = videoContainerView.frame
    videoContainerView.addSubview(controllsContainerView)
    controllsContainerView.addSubview(activityIndicatorView)
    activityIndicatorView.centerXAnchor.constraint(equalTo: videoContainerView.centerXAnchor).isActive = true
    activityIndicatorView.centerYAnchor.constraint(equalTo: videoContainerView.centerYAnchor).isActive = true

}

现在,视频需要8-15秒才能开始播放。我必须改善加载时间。 在网站上,视频在3秒后开始播放。

1 个答案:

答案 0 :(得分:0)

我发现我正在尝试播放的视频文件由于渲染时的高帧率而非常大。 (每个视频剪辑超过200 MB)

我将视频重新渲染为30帧速率,这使得文件更小,并大大提高了加载速度