Swift 在尝试播放视频时,在展开可选值时意外发现nil

Swift 在尝试播放视频时,在展开可选值时意外发现nil,swift,avfoundation,avplayer,Swift,Avfoundation,Avplayer,我正在使用AVFoundationkit播放本地视频 我尝试了以下代码: import UIKit import AVFoundation class ViewController: UIViewController { var player: AVPlayer? @IBOutlet weak var videoViewContainer: UIView! override func viewDidLoad() { super.viewDidLoad() initial

我正在使用
AVFoundation
kit播放本地视频

我尝试了以下代码:

import UIKit
import AVFoundation

class ViewController: UIViewController {

var player: AVPlayer?

@IBOutlet weak var videoViewContainer: UIView!


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

func initializeVideoPlayerWithVideo() {

    // get the path string for the video from assets
    let videoString:String? = Bundle.main.path(forResource: "Air Bike", ofType: "mov")
    guard let unwrappedVideoPath = videoString else {return}

    // convert the path string to a url
    let videoUrl = URL(fileURLWithPath: unwrappedVideoPath)

    // initialize the video player with the url
    self.player = AVPlayer(url: videoUrl)

    // create a video layer for the player
    let layer: AVPlayerLayer = AVPlayerLayer(player: player)

    // make the layer the same size as the container view
    layer.frame = videoViewContainer.bounds

    // make the video fill the layer as much as possible while keeping its aspect size
    layer.videoGravity = AVLayerVideoGravity.resizeAspectFill

    // add the layer to the container view
    videoViewContainer.layer.addSublayer(layer)
}


@IBAction func playVideoButtonTapped(_ sender: UIButton) {
    // play the video if the player is initialized
    player?.play()
}
}
我尝试了几种不同的方法,但仍然收到相同的错误消息。有人能告诉我如何解决这个问题吗

**use** 

layer.frame = videoViewContainer.layer.bounds 
而不是

 // make the layer the same size as the container view
    layer.frame = videoViewContainer.bounds

这是我的视频名称jagdeep和type.MOV,它在我这边工作。使用类型mov的代码中存在问题。我只是将一个视频名称jagdeep.MOV拖到我的项目中,然后使用您的代码。没问题

谢谢你的回复,似乎找不到文件这里是我项目的截图,这可能是视频位于我的主文件夹中的原因

我在这里没有看到任何不安全的可选展开,除了这一行
@ibvar videoViewContainer:UIView,您是否正确设置了插座??如果您不确定,请尝试在
viewdiload
中打印
videoViewContainer
是插座设置正确我已将它们移除,然后再次连接它们仍然相同还有什么可以尝试的吗?感谢您得到崩溃的哪一行???是
videoURL
正确分配了值吗?这里的“第41行”是哪一行?仍然是第41行islayer.frame=videoViewContainer.boundsOk,所以我在构建阶段添加了该文件,再次得到了相同的错误消息…..我只是不明白有什么可能出错。。。。。
func initializeVideoPlayerWithVideo() {

    guard let path = Bundle.main.path(forResource: "jagdeep", ofType:".MOV") else {
        debugPrint("video.m4v not found")
        return
    }

    self.player = AVPlayer(url: URL(fileURLWithPath: path))

    // create a video layer for the player
    let layer: AVPlayerLayer = AVPlayerLayer(player: player)

    // make the layer the same size as the container view
    layer.frame = videoViewContainer.bounds

    // make the video fill the layer as much as possible while keeping its aspect size
    layer.videoGravity = AVLayerVideoGravity.resizeAspectFill

    // add the layer to the container view
    videoViewContainer.layer.addSublayer(layer)

    player?.play()
}