Swift 2.0 的 AVPlayer 播放

AVPlayer playback with Swift 2.0

我在播放视频时遇到问题。我想要的只是在显示启动图像后播放视频。我遵循了这个 (https://www.youtube.com/watch?v=iYATp9--VIM) 教程,但是当我模拟时,我收到了一个空白屏幕。

代码如下:

import UIKit
import AVKit
import AVFoundation

class ViewController: UIViewController {

    @IBOutlet var videoView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.setupView()  
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func setupView() {
        let path = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("streetview", ofType: "mov")!)

        let player = AVPlayer(URL:path)

        let newLayer = AVPlayerLayer(player: player)
        newLayer.frame = self.videoView.frame
        self.videoView.layer.addSublayer(newLayer)
        newLayer.videoGravity = AVLayerVideoGravityResizeAspectFill

        player.play()
    }
}

如果可以请帮帮我!

如果您只是看到黑屏,这通常表示视频源存在问题。我会检查您是否正确拼写了文件名,并且它确实是一个 .mov 文件。

我使用此代码启动 AVPlayer 并播放我的视频。它播放来自互联网 URL 的视频。我把它放在我的按钮函数中,所以当用户按下我的播放按钮时会发生:

let videoURL = NSURL(string: "YOUR URL") {
let player = AVPlayer(URL: videoURL)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.presentViewController(playerViewController, animated: true) {

    playerViewController.player!.play()

}

我可以确认这在 Swift 2.0

中对我有用