簡體   English   中英

Swift - AVPlayer 加載失敗,錯誤為 Error Domain=NSURLErrorDomain Code=-999“已取消”

[英]Swift - AVPlayer load failed with error Error Domain=NSURLErrorDomain Code=-999 "cancelled"

我嘗試從 url 字符串播放視頻。 但是我在問題標題中遇到了一些錯誤。

我在下面嘗試這個代碼。 videoPath 是一個 url 字符串。

let videoURL = URL(string: videoPath)
        let player = AVPlayer(url: videoURL!)
        let playerViewController = AVPlayerViewController()
        playerViewController.player = player
        self.present(playerViewController, animated: true) {
            playerViewController.player!.play()
        }

以下是錯誤日志:

加載失敗,錯誤為 Error Domain=NSURLErrorDomain Code=-999 "cancelled" UserInfo={NSErrorFailingURLStringKey= http://b...a.mp4 , NSErrorFailingURLKey= http://b...a.mp4 , _NSURLErrorRelatedURLSessionTaskErrorKey=( " LocalDataTask <841B2FFA-479B-4E5A-9BD3-D9207EAA0D32>.<2>" ), _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <841B2FFA-479B-4E5A-9BD3-D9207EAA0D39.

我設置了 info.plist --

<key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
        <key>NSExceptionDomains</key>
        <dict>
            <key>www.example.com</key>
            <dict>
                <key>NSExceptionAllowsInsecureHTTPLoads</key>
                <true/>
                <key>NSExceptionMinimumTLSVersion</key>
                <string>TLSv1.1</string>
                <key>NSIncludesSubdomains</key>
                <true/>
            </dict>
        </dict>
    </dict>

注意:問題出現在 10 分鍾長的視頻中。 這是正常的嗎?

你想在viewDidLoad展示AVPlayerViewController嗎? 如果是這樣,您可以在將視圖控制器的視圖添加到窗口層次結構后嘗試呈現它 - 例如viewDidAppear 請記住,當您導航回控制器時將調用viewDidAppear並且將再次觸發模態演示。

URL 是否需要設置 cookie? 我遇到了同樣的丟失 cookie 的問題。 您可以通過嘗試在隱身窗口中打開 url 來進行檢查。 如果它仍然可以正常運行,那么也許您可以通過以下方式對其進行調試 -

使用 URL 創建一個AVURLAsset對象,例如 -
AVURLAsset(url: <URL>, options:[])並將resourceLoader委托設置為self。 urlAsset?.resourceLoader.setDelegate(self, queue: .main)並實現函數

func resourceLoader(_ resourceLoader: AVAssetResourceLoader, shouldWaitForLoadingOfRequestedResource loadingRequest: AVAssetResourceLoadingRequest) -> Bool {
    return true
}

func resourceLoader(_ resourceLoader: AVAssetResourceLoader, shouldWaitForRenewalOfRequestedResource renewalRequest: AVAssetResourceRenewalRequest) -> Bool {
    return true
}

func resourceLoader(_ resourceLoader: AVAssetResourceLoader, didCancel loadingRequest: AVAssetResourceLoadingRequest) {

}

func resourceLoader(_ resourceLoader: AVAssetResourceLoader, shouldWaitForResponseTo authenticationChallenge: URLAuthenticationChallenge) -> Bool {
    return true
}

func resourceLoader(_ resourceLoader: AVAssetResourceLoader, didCancel authenticationChallenge: URLAuthenticationChallenge) {

}

但如果確實需要 cookie - 將它們設置在AVURLAsset對象中,例如let asset = AVURLAsset(url: videoURL, options: ["AVURLAssetHTTPHeaderFieldsKey": ["Cookie": "<YOUR TOKEN>"]])

原因可能是視頻中的元數據錯誤。 看看我回答的這個線程: AVPlayer HLS live stream IOS

轉碼后的視頻需要有profile baseline才能在 AVPlayer 中播放。 詳細看ffmpeg轉碼命令:

https://gist.github.com/chung-nguyen/d88e73e3cc8788878f5ffb8c232b4729

NSErrorFailingURLKey= http://b...a.mp4不管你有什么 ATS,帶有“http”前綴的 url 總是失敗。 在這里訪問我的答案

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM