gpt4 book ai didi

swift - AVPlayer 不允许我访问 URL

转载 作者:可可西里 更新时间:2023-10-31 23:58:30 24 4
gpt4 key购买 nike

我正在尝试使用 swift 4.0 编写非常简单的 Xcode OS X 程序 - 该程序可以构建并运行,但我在访问 URL 视频文件时遇到错误。可能与某处的特权或安全设置有关,但无法弄清楚在哪里。这是我在 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 文件:

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

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

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.

最佳答案

我在您对 question 发表评论后发现了您的问题我之前已经回答过了,所以让我们在这里继续:)

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

总而言之,您需要:

  1. AVPlayerView 添加到您的 Storyboard 并将其连接到您的 ViewController
  2. 设置你的ViewController
  3. 管理应用程序传输安全和传出连接。

更详细

1。将 AVPlayerView 添加到您的 Storyboard 并将其连接到您的 ViewController

AVKit Player View 拖到 Storyboard中的 View

Add AVKit player view

在您的 ViewController 类中,为 View 创建一个 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

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

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

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

这是我的完整的.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)

enable Outgoing Connections

如果我这样做,我会得到这样的结果:

The final result

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

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

关于swift - AVPlayer 不允许我访问 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47127618/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com