AVPlayer不允许我访问URL [英] AVPlayer won't let me access URL

查看:209
本文介绍了AVPlayer不允许我访问URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Swift 4.0实现非常简单的Xcode OS X程序-该程序可以构建并运行,但是访问URL视频文件时出现错误.可能与某处的特权或安全设置有关,但无法确定该位置.这是我在ViewController类下的程序:NSViewController:

I am Attempting very simple Xcode OS X program with swift 4.0 - this program builds and runs but I'm getting errors accessing the URL video file. Probably related to privilege or security setting somewhere but can't figure out where. Here is my program under class ViewController: NSViewController:

@IBOutlet var playerView: AVPlayerView!
//var url: URL!

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    // guard let url = URL(string: "https://devimages.apple.com.edgekey.net/samplecode/avfoundationMedia/AVF oundationQueuePlayer_HLS2/master.m3u8")
    //    else {
    //        return
    //    }

   let url1 = URL(string: "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4")


    let player = AVPlayer(url: url1!)
    // Create a new AVPlayer and associate it with the player view

    let playerView = self.playerView
    playerView?.player = player
    playerView?.player!.play();
}

我还使用以下内容修改了info.plist文件:

Also I have modified the info.plist file with the following:

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

我在尝试访问两个URL时遇到以下运行时错误 上面为什么?

I get the following runtime errors trying to access either of the URLs above why?

2017-11-05 10:12:21.757333-0800 AVPlayer [54381:5617593] startLogging: 日志记录开始... 2017-11-05 10:12:21.758024-0800 AVPlayer [54381:5617612] setMessageLoggingBlock:称为2017-11-05 10:12:21.758520-0800 AVPlayer [54381:5617612] initWithSessionInfo:XPC 连接中断2017-11-05 10:12:21.758842-0800 AVPlayer [54381:5617612] startConfigurationWithCompletionHandler: 无法获取远程对象代理:Error Domain = NSCocoaErrorDomain 代码= 4097到名为com.apple.rtcreportingd的服务的连接" UserInfo = {NSDebugDescription =与名为的服务的连接 com.apple.rtcreportingd} 2017-11-05 10:12:21.759003-0800 AVPlayer [54381:5617612] startConfigurationWithCompletionHandler: 无法获取远程对象代理:Error Domain = NSCocoaErrorDomain 代码= 4097到名为com.apple.rtcreportingd的服务的连接" UserInfo = {NSDebugDescription =与名为的服务的连接 com.apple.rtcreportingd} 2017-11-05 10:12:21.925221-0800 AVPlayer [54381:5617654]应用程序传输安全性已阻止明文 HTTP(http://)资源加载,因为它是不安全的.暂时的 可以通过您应用的Info.plist文件配置例外.

2017-11-05 10:12:21.757333-0800 AVPlayer[54381:5617593] startLogging: logging starts... 2017-11-05 10:12:21.758024-0800 AVPlayer[54381:5617612] setMessageLoggingBlock: called 2017-11-05 10:12:21.758520-0800 AVPlayer[54381:5617612] initWithSessionInfo: XPC connection interrupted 2017-11-05 10:12:21.758842-0800 AVPlayer[54381:5617612] startConfigurationWithCompletionHandler: Failed to get remote object proxy: Error Domain=NSCocoaErrorDomain Code=4097 "connection to service named com.apple.rtcreportingd" UserInfo={NSDebugDescription=connection to service named com.apple.rtcreportingd} 2017-11-05 10:12:21.759003-0800 AVPlayer[54381:5617612] startConfigurationWithCompletionHandler: Failed to get remote object proxy: Error Domain=NSCocoaErrorDomain Code=4097 "connection to service named com.apple.rtcreportingd" UserInfo={NSDebugDescription=connection to service named com.apple.rtcreportingd} 2017-11-05 10:12:21.925221-0800 AVPlayer[54381:5617654] 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 found your question after you commented on a question I had answered previously, so lets continue here :)

我可以通过将我对这个问题的原始答案与上面@callam提供的答案结合在一起来使您的代码正常工作,以便...将正确答案归功于他:)

I can get your code working by combining my original answer to that question with the answer provided by @callam above so...give him the credit for the correct answer :)

总结,您需要:

  1. 在情节提要中添加AVPlayerView并将其连接到ViewController
  2. 设置您的ViewController
  3. 管理应用传输安全性和传出连接.
  1. Add a AVPlayerView to your Storyboard and connect it to your ViewController
  2. Setup your ViewController
  3. Manage App Transport Security and the outgoing connection.

更详细

将一个AVKit Player View拖到情节提要中的视图中

Drag a AVKit Player View to your view in the storyboard

在您的ViewController类中,为视图创建outlet:

In your ViewController class, create an outlet for the view:

 @IBOutlet weak var playerView: AVPlayerView!

并连接它.

这是我的代码:

import Cocoa
import AVKit

class ViewController: NSViewController {

    @IBOutlet weak var playerView: AVPlayerView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let fileURL = URL(string: "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4")
        let avAsset = AVURLAsset(url: fileURL!, options: nil)

        let playerItem = AVPlayerItem(asset: avAsset)
        let videoPlayer = AVPlayer(playerItem: playerItem)
        playerView.player = videoPlayer
        videoPlayer.play()
    }
}

似乎不熟悉:)

请注意,我在这里创建了videoPlayer,不需要我使用您在评论中提到的let playerView = self.playerView.

Notice I create the videoPlayer right here, no need to for me to use the let playerView = self.playerView you mention in your comments.

如果仅执行这两个步骤,则最终会出现无法在控制台中播放视频和错误的播放器视图.因此,您需要将最后一块拼图添加到上面的@callam所提供的拼图中.

If you just do these two steps, you end up with a player view that cannot play video and errors in your console. So you need to add the final piece to the puzzle, kindly provided by @callam above.

首先,如果不能使用HTTPS连接,则必须在info.plist文件中允许HTTP连接.

First, if you cannot use a HTTPS connection you must allow HTTP connections in your info.plist file.

这是我的全部.plist文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleDevelopmentRegion</key>
    <string>$(DEVELOPMENT_LANGUAGE)</string>
    <key>CFBundleExecutable</key>
    <string>$(EXECUTABLE_NAME)</string>
    <key>CFBundleIconFile</key>
    <string></string>
    <key>CFBundleIdentifier</key>
    <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleName</key>
    <string>$(PRODUCT_NAME)</string>
    <key>CFBundlePackageType</key>
    <string>APPL</string>
    <key>CFBundleShortVersionString</key>
    <string>1.0</string>
    <key>CFBundleVersion</key>
    <string>1</string>
    <key>LSMinimumSystemVersion</key>
    <string>$(MACOSX_DEPLOYMENT_TARGET)</string>
    <key>NSHumanReadableCopyright</key>
    <string>Copyright © 2017 :) </string>
    <key>NSMainStoryboardFile</key>
    <string>Main</string>
    <key>NSPrincipalClass</key>
    <string>NSApplication</string>
    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>
</dict>
</plist>

重要的部分是NSAppTransportSecurity部分

最后,您必须在项目的Capabilities下启用Outgoing Connections (Client)

And then finally, you must enable Outgoing Connections (Client) under Capabilities of your project

如果我这样做,我将得到以下结果:

If I do that, I end up with this:

但是请注意,我仍然在控制台中看到错误.

But notice though, that I still see errors in the console.

希望您可以按照以下步骤获得相同的结果:)

Hope you are able to get to the same result following these steps :)

这篇关于AVPlayer不允许我访问URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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