AVPlayer不允许我访问URL

时间:2017-11-05 23:08:25

标签: swift xcode avplayer

我正在使用swift 4.0尝试非常简单的Xcode OS X程序 - 这个程序构建并运行但是我在访问URL视频文件时遇到错误。可能与特权或安全设置有关,但无法弄清楚在哪里。这是我在ViewController类下的程序:NSViewController:

int main ()
{
  std::vector<int> myvector (3,100);
  std::vector<int>::iterator it;

  it = myvector.begin();
  it = myvector.insert ( it , 200 );

  myvector.insert (it,2,300);

  // "it" no longer valid, get a new one:
  it = myvector.begin();

  std::vector<int> anothervector (2,400);
  myvector.insert (it+2,anothervector.begin(),anothervector.end());

  int myarray [] = { 501,502,503 };
  myvector.insert (myvector.begin(), myarray, myarray+3);

  std::cout << "myvector contains:";
  for (it=myvector.begin(); it<myvector.end(); it++)
    std::cout << ' ' << *it;
  std::cout << '\n';

  return 0;
}

我还修改了info.plist文件,其中包含以下内容:

@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();
}

尝试访问其中一个网址时出现以下运行时错误  以上为什么?

  

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:   无法获取远程对象代理:错误域= NSCocoaErrorDomain   Code = 4097“连接到名为com.apple.rtcreportingd的服务”   UserInfo = {NSDebugDescription =指向服务的连接   com.apple.rtcreportingd} 2017-11-05 10:12:21.759003-0800   AVPlayer [54381:5617612] startConfigurationWithCompletionHandler:   无法获取远程对象代理:错误域= NSCocoaErrorDomain   Code = 4097“连接到名为com.apple.rtcreportingd的服务”   UserInfo = {NSDebugDescription =指向服务的连接   com.apple.rtcreportingd} 2017-11-05 10:12:21.925221-0800   AVPlayer [54381:5617654] App Transport Security已阻止明文   HTTP(http://)资源加载,因为它不安全。临时   可以通过应用的Info.plist文件配置例外。

2 个答案:

答案 0 :(得分:3)

在您评论我之前回复的question之后,我找到了您的问题,让我们继续这里:)

我可以通过将我对该问题的原始答案与@callam上面提供的答案相结合来使代码工作,所以......给予他正确答案的信誉:)

总结一下,您需要:

  1. AVPlayerView添加到故事板并将其与ViewController
  2. 相关联
  3. 设置ViewController
  4. 管理App Transport Security和传出连接。
  5. 更详细

    1。将AVPlayerView添加到故事板并将其连接到ViewController

    AVKit Player View拖到故事板中的视图

    Add AVKit player view

    ViewController课程中,为视图创建outlet

     @IBOutlet weak var playerView: AVPlayerView!
    

    连接它。

    2。设置您的ViewController

    这是我的代码:

    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

    如果您只是执行这两个步骤,最终会得到一个无法在控制台中播放视频和错误的播放器视图。所以你需要将最后一块添加到拼图中,由上面的@callam友情提供。

    3。管理App Transport Security和传出连接。

    首先,如果您无法使用HTTPS连接,则必须在HTTP文件中允许info.plist个连接。

    这是我的.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部分

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

    enable Outgoing Connections

    如果我这样做,我最终会这样:

    The final result

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

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

答案 1 :(得分:2)

即使您启用了任意加载,尝试使用不安全的HTTP进行连接也会失败。

将您网址中的协议更改为https://或将域名添加到NSExceptionDomains

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

https://developer.apple.com/library/content/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html#//apple_ref/doc/uid/TP40009251-SW35

我相信可以通过在应用功能

中选择Outgoing Connections来解决XPC错误

App Sandbox

https://developer.apple.com/library/content/documentation/Miscellaneous/Reference/EntitlementKeyReference/Chapters/EnablingAppSandbox.html