0

All I am trying to do is have a view Controller that has button. When pressed it will play a video.

My code has a runtime bad instruction error. The app can build but when the button is pressed the error message comes up. The error message is in line "let videoURL = NSURL(fileURLWithPath: filePath!)".

import UIKit
import AVKit
import AVFoundation

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
@IBAction func playVideo(_ sender: UIButton) {

    playLocalVideo()

}
func playLocalVideo(){
    let filePath = Bundle.main.path(forResource: "t", ofType: "mp4")
    let videoURL = NSURL(fileURLWithPath: filePath!)
    let player = AVPlayer(url: videoURL as URL)
    let playViewController = AVPlayerViewController()

    playViewController.player = player
    self.present(playViewController, animated: true) {() -> Void in playViewController.player!.play()


} }  }
  • Your issue is that `filePath` is `nil`. Read the detailed question and answer to the duplicate question to learn how to do this properly. – rmaddy Oct 27 '16 at 16:18
  • I read the texts its very long and confusing any chance your could just spare me the answer? –  Oct 27 '16 at 22:37
  • I already told you the specific problem in my first comment. And as a Swift programmer, it is extremely important that you fully understand optional and force unwrapping. – rmaddy Oct 27 '16 at 22:38

0 Answers0