在Cocoa OS X AVPlayer中播放HLS(m3u8)-Swift [英] Playing HLS (m3u8) in Cocoa OS X AVPlayer - Swift

查看:204
本文介绍了在Cocoa OS X AVPlayer中播放HLS(m3u8)-Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我正在尝试使用Cocoa Swift中的AVPlayer播放m3u8(HLS实时流).我是该语言的新手,因此基本上可以使用一些示例代码来播放本地视频文件和尝试对其进行修改以播放实时流...但是请改用此方法:

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!

推荐答案

我试图将您的代码粘贴到新的OS X项目(或macOS,因为我们必须立即开始调用它:))

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.

如果尚未启用调试区域,请在Xcode顶部启用它

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

然后您需要确保同时显示控制台,这是在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 :)

此消息基本上告诉您Apple已阻止对HTTP的访问.这是OS X 10.11和iOS 9中引入的,但可以禁用.

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.

如控制台中所述:

可以通过应用的Info.plist文件配置临时例外.

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

这意味着您应该在info.plist文件中添加一个新密钥.

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

您可以将其添加为原始" plist数据,如下所示:

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

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

或者您可以将其添加到plist编辑器中,如下所示:

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.

这篇关于在Cocoa OS X AVPlayer中播放HLS(m3u8)-Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆