无法播放本地视频文件

时间:2015-10-23 22:45:40

标签: swift tvos

我尝试使用Xcode 7.1播放本地视频。我想要的只是视频在视图和播放时加载全屏。

import UIKit
import AVFoundation
import AVKit

class myClass: AVPlayerViewController {
    private func playVideo() {
        if let path = NSBundle.mainBundle().pathForResource(“video”, ofType: "mp4") {
            let url = NSURL(fileURLWithPath: path)
            player = AVPlayer(URL: url)
            player?.play()
        }
        else {
            print("Oops, something wrong when playing video.mp4")
        }
    }

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        playVideo() 
    } 
}

1 个答案:

答案 0 :(得分:0)

我设法找到了解决办法。

import Foundation
import UIKit
import AVKit

class PlayerViewController: AVPlayerViewController, AVPlayerViewControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    func playVideo() {

        if let path = NSBundle.mainBundle().pathForResource("video", ofType: "mp4") {
            let url = NSURL(fileURLWithPath: path)
            player = AVPlayer(URL: url)
            player?.play()
        }
        else {
            print("Oops, something wrong when playing video.mp4")
        }
    }

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        playVideo()
    }
}