Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift AVPlayer Won't Player MP4: Play Button With Line Through It

enter image description here

I am trying to player an mp4 video from the local file system on an ios app. The video is about an hour long and can be played on quicktime. I am able to play an mp4 if it is from the web (NSURL(string: "http:domain.com/vid.mp4")). I am playing the video like below:

    let steamingURL:NSURL = NSURL(string: video.filePath)! //valid path
    let player = AVPlayer(URL: steamingURL)
    player.allowsExternalPlayback = false

    PlayWorkoutViewController.playerController = AVPlayerViewController()
    PlayWorkoutViewController.playerController.player = player
    self.addChildViewController(PlayWorkoutViewController.playerController)
    self.view.addSubview(PlayWorkoutViewController.playerController.view)
    PlayWorkoutViewController.playerController.view.frame = videoContainerView.frame
    PlayWorkoutViewController.playerController.showsPlaybackControls = false
    player.play()

Do I need to change some of the settings to play the longer video? Thanks.

like image 888
Alex Pelletier Avatar asked Dec 23 '15 02:12

Alex Pelletier


People also ask

What is AVPlayer in Swift?

An object that provides the interface to control the player's transport behavior.

Can we play video in Swift?

New in iOS 14SwiftUI's VideoPlayer view lets us playback movies from any URL, local or remote. It comes from the AVKit framework, so you should make sure and add import AVKit before trying it out. Reminder: You need to add import AVKit to your Swift file to use this.


1 Answers

Check the steamingURL is nil or not first

If you want to use NSURL(string: "xxx") initialiser please check the string starts with file://, otherwise use NSURL(fileURLWithPath: "xxxx") instead.

like image 154
jessex Avatar answered Nov 01 '22 04:11

jessex