Search code examples
swiftmacoscocoaavplayerhttp-live-streaming

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


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:

https://i.sstatic.net/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!


Solution

  • 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

    enable debug area

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

    enable console

    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:

    editing plist

    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.