美文网首页iOS Developer
Swift3.0 Day03 AVPlayer+NSBundle

Swift3.0 Day03 AVPlayer+NSBundle

作者: 赵镇 | 来源:发表于2016-12-11 14:11 被阅读283次

    由于前几天踩到坑了,其他事情也比较忙,所以停更了,今天会把前几天的补上。今天要说的是利用AVPlayer+Bundle来实现本地视频的播放
    首先实现本地视频播放主要是利用AVPlayerViewController和AVPlayer两个类来实现的。
    首先是引入框架

    import AVKit
    import AVFoundation
    

    初始化##

        var playViewController = AVPlayerViewController()
        var playerView = AVPlayer()
    

    在这里补充说一下Tableview 注册Xib的Cell的例子

            //在viewDidLoad方法中
            let cellNib = UINib(nibName: "VideoViewCell", bundle: nil)
            playtableView.register(cellNib, forCellReuseIdentifier: "VideoViewCell")
            //在TableView的代理中
            let cell = playtableView.dequeueReusableCell(withIdentifier: "VideoViewCell") as! VideoViewCell
    

    然后是从bundle中如何查找到视频(资源),这一点与Swift2.3和OC还是有很大区别的

            let path = Bundle.main.path(forResource: "emoji", ofType:"mp4")
    

    其实,我在这里遇到了一个坑,因为貌似Xcode8.1添加资源默认是没有勾上add to Target的这个选项的,导致我一直找不到这个资源,工程崩溃,所以在添加资源的时候这个选项一定得勾上

    视频播放###

            playerView = AVPlayer(url: URL(fileURLWithPath: path!) as URL)
            
            playViewController.player = playerView
            
            self.present(playViewController, animated: true) {
                self.playViewController.player?.play()
    

    好了,今天的内容就是这么多,代码地址,请戳AVPlayer播放本地视频

    相关文章

      网友评论

        本文标题:Swift3.0 Day03 AVPlayer+NSBundle

        本文链接:https://www.haomeiwen.com/subject/wkkpmttx.html