Unable to play .mp4 video file using AVPlayer

5.6k views Asked by At

I am using the AVPlayer to play the video from url.

func playVideoFromUrl(urlString: String){

        let videoURL = URL(string: urlString)
        let player = AVPlayer(url: videoURL!)
        let playerViewController = AVPlayerViewController()
        playerViewController.player = player

        let delegate : AppDelegate = UIApplication.shared.delegate as! AppDelegate
        delegate.window?.rootViewController?.present(playerViewController, animated: true) {
            playerViewController.player!.play()
        }
    }

The url of video:- "http://gsn-input-dev.s3.amazonaws.com/public/video/00229/9229d4ad2a0c547e7bfa51e3fbef806f.mp4"

I am not getting any warning at console. Below is the screenshot of simulator. What I am doing wrong?

enter image description here

4

There are 4 answers

1
iOS_MIB On

Have you tried adding arbitrary load flag in your info.plist file as i see the url is a HTTP url and to make the HTTP url work, we need to specify it in app transportation security via info.plist as follows

enter image description here

Please try doing this as video is working perfectly fine with you code.

Read following document for more info on App Transportation Security

I hope this will help you :)

0
Miguel Tepale On

I recently found out that a device running iOS 14 using AVPlayer cannot play .mp4 files. A compatible video format that works on AVPlayer is .mov.

0
Jay Dee On

I also have had a trouble playing .mp4 files. When I made the URL end with .mp4 it works.

2
Imrul Kayes On

I have accomplished what you've wanted to do. Here is my approach: First import AVFoundation, AVKit(I believe you had) then I have configured my viewcontroller like this

   @IBOutlet weak var avPlayerView: UIView! // drag a UIView to view controller
   var videoPlayer = AVPlayer()
   var avPlayerViewController = AVPlayerViewController()

   override func viewDidLoad() {
     super.viewDidLoad()

     let videoURL = URL(string: "http://gsn-input-dev.s3.amazonaws.com/public/video/00229/9229d4ad2a0c547e7bfa51e3fbef806f.mp4")

    self.playVideo(url: videoURL!)

}

playVideo function is configured like below

  func playVideo(url: URL) {
       self.videoPlayer = AVPlayer(url: url)
       self.avPlayerViewController = AVPlayerViewController()
       self.avPlayerViewController.player = self.videoPlayer
       avPlayerViewController.view.frame = avPlayerView.frame
       self.addChildViewController(avPlayerViewController)
       self.view.addSubview((avPlayerViewController.view)!)

   }

Open your info.plist as Source Codeenter image description here

then add this to info.plist

   <key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

screen shot is given below

enter image description here