使用插件在视图控制器中缓存视频

时间:2017-06-12 21:29:38

标签: ios swift caching

几周前我开始学习swift,现在我正在进行一些有点棘手的事情:我想构建一个使用UIPageViewController的简单应用程序。每个页面都应包含一个视频,该视频使用名为Player的插件从服务器加载。由于我希望单个视频成为页面上的播放器,因此我使用存储所有URL的数组。然后,这些URL用于在另一个类中设置值。如果设置了此类中的值(didSet),则使用插件加载视频。所有这一切都很好!要了解我的所作所为,您可以点击此链接至this youtube tutorial,我将其用作定位。

但是,我遇到了一个我已经怀疑过的问题:视频是重新加载每当有人点击一个已经打开的页面并且视频已经加载一次。显然,我不希望视频每次都重新加载,这就是为什么我在awesome-swift下寻找swift的缓存库的原因。第一个建议(HanekeSwift)不知何故对我不起作用,每次我试图包含它时都给了我一堆错误。因此,我尝试使用提供的第二个选项来解决我的问题:Carlos这不会导致任何错误(这是一个德国新闻机构的图书馆所以听起来也很专业)。尽管如此,由于我对swift很陌生,我无法完全理解这个插件。因为我很新,我不懂很多东西......

我需要在代码中添加什么来缓存视频?我需要为此创建另一个类吗?你会怎么建议我这样做?我将在下面提供我现在所拥有的......并且对于长期(和新的)问题感到遗憾,但我真的无法弄明白。

代码

PageViewController

//viewDidLoad
override func viewDidLoad() {

    super.viewDidLoad()

    //set datasourcee to self for now
    dataSource = self

    view.backgroundColor = .white

    let postViewController = PostViewController()
    postViewController.video = videos.first
    let viewControllers = [postViewController]

    //set first view controller
    setViewControllers(viewControllers, direction: .forward, animated: true, completion: nil)

}

//videos array
let videos = ["https://link.to/testvideo.mov", "https://link.to/testvideo.mov", "https://link.to/testvideo.mov", "https://link.to/testvideo.mov"]

//after page
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {

    let currentIndexString  = (viewController as! PostViewController).index
    let currentIndex        = indec.index(of: currentIndexString!)

    //next page possible?
    if currentIndex! < indec.count - 1 {

        let postViewController = PostViewController()
        postViewController.video            = videos[currentIndex! + 1]

        return postViewController

    }

    return nil

}

//before page
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {

    let currentIndexString  = (viewController as! PostViewController).index
    let currentIndex        = indec.index(of: currentIndexString!)

    //previous page possible?
    if currentIndex! > 0 {

        //template
        let postViewController = PostViewController()
        postViewController.video            = videos[currentIndex! - 1]

        return postViewController

    }

    return nil

}

PostViewController

var video:String? {

    didSet {

        print(video ?? String())
        setupPlayer()

    }

}

//orienting myself by the example provided by "Player" plugin from here on (https://github.com/piemonte/Player/blob/master/Project/Player/ViewController.swift)
fileprivate var player = Player()

deinit {

    self.player.willMove(toParentViewController: self)
    self.player.view.removeFromSuperview()
    self.player.removeFromParentViewController()

}

override func viewDidLoad() {

    super.viewDidLoad()

    view.backgroundColor = .blue

}

func setupPlayer() {

    self.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]

    self.player.playerDelegate = self as? PlayerDelegate
    self.player.playbackDelegate = self as? PlayerPlaybackDelegate
    self.player.view.frame = self.view.bounds

    self.addChildViewController(self.player)
    self.view.addSubview(self.player.view)
    self.player.didMove(toParentViewController: self)

    self.player.url = URL(string: video!)

    self.player.playbackLoops = true

}

0 个答案:

没有答案