简体   繁体   中英

xcode 8 || AVPlayer is showing video however it's not playing

I have this simple code to play an online video. After doing the HTTP permission option, it did start to show the video and I can scroll through it, however, it doesn't play at any point. Here is the code. I would appreciate if someone could help me with this.

regards, A Beginner Coder

import UIKit
import AVKit
import AVFoundation

class ViewController: UIViewController {

var playerViewController = AVPlayerViewController()
var playerView = AVPlayer()



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


}
override func viewDidAppear(_ animated: Bool) {
    let movieUrl: NSURL? = NSURL(string: "http://clips.vorwaerts-gmbh.de/VfE_html5.mp4")
    playerView = AVPlayer(url: movieUrl as! URL)
    playerViewController.player = playerView

    self.present(playerViewController, animated: true){
        self.playerViewController.player?.play()
    }

}

}

When I ran your code in a sample project the debugger prints:

"App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file."

The two options I found were:

1) Change the scheme to https:// in your URL

or

2) Adjust the App Transport Security settings in your Info.plist

I successfully played the video using both options with your code.

It turns out that the only thing i had done was, the last line had

"self.playerViewController.player?.play()" ----->>> "?" and when i changed it to --------->>> "!"

IT MAGICALLY STARTED WORKING....

i have seen the meme on the internet where they talk about coders not knowing why the code works and why it doesn't... same happpened to me i guess.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM