使用tvOS从本地文件播放视频

时间:2015-09-25 18:58:05

标签: swift url tvos

当我的应用程序在Apple TV上打开时,我想播放一个小视频(20秒)。但我无法找到如何从本地文件中实现URL。

这是我迄今为止所做的,但遗憾的是它不起作用:

    override func viewDidLoad() {
        super.viewDidLoad()

        // I know this is how to play a file from a webserver:
        // player = AVPlayer(URL: NSURL(string: "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")!)

        // But I can't find how to set the URL to a local file in the app
        let path = NSBundle.mainBundle().pathForResource("bunny", ofType:"mp4")
        let url = NSURL.fileURLWithPath(path!)
        player = AVPlayer(URL: url)

        player?.play()

    }

2 个答案:

答案 0 :(得分:1)

您的代码似乎很好。也许,你应该试试这个:

  

在Project Navigator中,选择Project Root>你的目标>   构建阶段>复制捆绑资源。检查您的视频是否正常   在那里,如果没有点击加号来添加它。

来源:http://www.brianjcoleman.com/tutorial-play-video-swift/

答案 1 :(得分:0)

我想列出最紧凑的方法以及所有四个障碍:

  

导入 AVKit ,从 viewDidAppear 调用playVideo()函数,观看   正确的捆绑软件访问权限,并确保文件具有   如Ennio所述正确的目标成员身份

private func playVideo() {

    guard let path = Bundle.main.path(forResource: "video", ofType:"mov") else {
        debugPrint("video.mov not found")
        return
    }
    let player = AVPlayer(url: URL(fileURLWithPath: path))

    // Create a new AVPlayerViewController and pass it a reference to the player.
    let controller = AVPlayerViewController()
    controller.player = player

    // Modally present the player and call the player's play() method when complete.
    present(controller, animated: true) {
        player.play()
    }
}