Playing HLS (m3u8) in Cocoa OS X AVPlayer - Swift

匿名 (未验证) 提交于 2019-12-03 01:01:02

问题:

Basically I am trying to play an m3u8 (HLS Live Stream) using AVPlayer in Cocoa Swift. I'm relatively new to the language, so basically grabbed some example code for playing local video files and tried modifying it to play a live stream... But get this instead:

http://i.stack.imgur.com/bU9GM.png

This is what I got so far (commented lines are to play local file, which does work):

import Cocoa import AVKit import Foundation import AVFoundation  class ViewController: NSViewController {      @IBOutlet weak var playerView: AVPlayerView!      var videoPlayer:AVPlayer!      override func viewDidLoad() {         super.viewDidLoad()          //let path = NSBundle.mainBundle().pathForResource("sample", ofType: "mov")         //var fileURL = NSURL(fileURLWithPath: path!)         let fileURL = NSURL(string: "http://qthttp.apple.com.edgesuite.net/1010qwoeiuryfg/sl.m3u8")         let avAsset = AVURLAsset(URL: fileURL!, options: nil)          let playerItem = AVPlayerItem(asset: avAsset)         videoPlayer = AVPlayer(playerItem: playerItem)         playerView.player = videoPlayer         videoPlayer.play()     }      override var representedObject: AnyObject? {         didSet {             // Update the view, if already loaded.         }     } } 

Any help on how to make this code work, or lead me in the right direction is much appreciated!

回答1:

I tried to paste your code into a new OS X project (or macOS as we must start calling it now :))

When I launched the project I got this error in the console:

2016-06-21 09:09:27.860 Videoplayer[2494:169209] 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. 

I don't know if you have the console enabled, it is the bottom part of your screen, provided you have the debug area enabled.

If you haven't got the debug area enabled, then you enable it in the top of Xcode

And then you need to make sure that you show the console as well, which is done in the bottom part of Xcode:

OK, now you can see the error, then how to fix it :)

This message basically tells you that Apple has blocked access to HTTP. This was introduced in OS X 10.11 and iOS 9, but can be disabled.

As it says in the console:

Temporary exceptions can be configured via your app's Info.plist file.

This means that you should add a new key to your info.plist file.

You can add it as "raw" plist data, which looks like this:

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

Or you can add it in the plist editor, where it looks like this:

This question and the great answers describes the process better that I can

If I do this, I am able to get your code running and can watch the live stream, so I hope this helps you too.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!