2

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

pkc456
  • 8,350
  • 38
  • 53
  • 109

4 Answers4

5

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

Imrul Kayes
  • 618
  • 1
  • 10
  • 17
  • I really appreciate your hard work. Please accept my up vote. Thanks! – pkc456 Oct 09 '17 at 07:15
  • Just add an "s" and use https to avoid all that NSAppTransportSecurity as it's not recommended to set that to NSAllowsArbitraryLoads. – Groot May 06 '21 at 13:38
4

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 :)

iOS_MIB
  • 1,885
  • 13
  • 24
0

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.

Miguel Tepale
  • 289
  • 5
  • 15
0

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

user2878850
  • 2,446
  • 1
  • 18
  • 22