【问题标题】:AVFoundation over Cellular Data基于蜂窝数据的 AVFoundation
【发布时间】:2021-08-17 02:46:12
【问题描述】:

当我使用下面的代码并通过 Wifi 从 Firebase 拉取我的 https 视频链接时,一切都很顺利,视频立即播放,问题为零。当我在蜂窝网络上使用相同的代码时,一切都变得非常缓慢,就像视频暂停并且永远加载一样。

如果它是从文件播放的,无论我使用的是蜂窝网络还是 Wifi 都没有关系。这里有什么问题?

数据模型:

class Video {

    var httpsStr: String?
    var videoURL: URL?        

    convenience init(dict: [String: Any] {
        self.init()

        if let httpsStr = dict["httpsStr"] as? String {

            self.httpsStr = httpsStr

            let url = URL(string: httpsStr)!
            let assetKeys = [ "playable", "duration"]

            let asset = AVURLAsset(url: url)
            asset.loadValuesAsynchronously(forKeys: assetKeys, completionHandler: {
                DispatchQueue.main.async {
                
                    self.videoURL = asset.url

                    // save videoURL to FileManager to play video from disk
                }
            })
        }
    } 
}

Firebase 拉取:

ref.observeSingleEvent(of: .value) { (snapshot) in

    guard let dict = snapshot.value as? [String: Any] else { return }

    let video = Video(dict: dict)
    self.video = video

    DispatchQueue.main.asyncAfter(deadline: .now() + 2, execute: {
        self.playVideo()
    }
}

播放视频:

func playVideo() {

    // init AVPlayer ...

    guard let videoURL = self.video.videoURL else { return }

    let lastPathComponent = videoURL.lastPathComponent
    let file = FileManager...appendingPathComponent(lastPathComponent)

    if FileManager.default.fileExists(atPath: file.path) {

        let asset = AVAsset(url: file)
        play(asset)

    } else {

        let asset = AVAsset(url: videoURL)
        play(asset)
    }
}

func play(_ asset: AVAsset) {
    
    self.playerItem = AVPlayerItem(asset: asset)

    self.player?.automaticallyWaitsToMinimizeStalling = false // I also set this to true
    self.playerItem?.preferredForwardBufferDuration = TimeInterval(1)

    self.player?.replaceCurrentItem(with: playerItem!)
    // play video
}

【问题讨论】:

    标签: ios swift firebase-realtime-database avfoundation


    【解决方案1】:

    我关注了这个answer,现在在蜂窝数据上一切似乎都很顺利。我需要在assetKeys 中包含tracks 属性。

    您使用 AVURLAsset 从 URL 创建资产。创建资产, 但是,并不一定意味着它已经可以使用了。成为 使用时,资产必须已加载其轨道。

    class Video {
    
        var httpsStr: String?
        var videoURL: URL?        
    
        convenience init(dict: [String: Any] {
            self.init()
    
            if let httpsStr = dict["httpsStr"] as? String {
    
                self.httpsStr = httpsStr
    
                let url = URL(string: httpsStr)!
                let assetKeys = ["playable", "duration", "tracks"] // <----- "tracks" added here
    
                let asset = AVURLAsset(url: url)
                asset.loadValuesAsynchronously(forKeys: assetKeys, completionHandler: {
    
                    var error: NSError? = nil
                    let status = asset.statusOfValue(forKey: "tracks", error: &error)
                    switch status {
    
                        case .loaded:
                            // Sucessfully loaded, continue processing
                            DispatchQueue.main.async {
                    
                                self.videoURL = asset.url
                                // save videoURL to FileManager to play video from disk
                            }
    
                        case .failed: 
                            // Examine NSError pointer to determine failure
                            print("Error", error?.localizedDescription as Any)
    
                        default: 
                            // Handle all other cases
                            print("default")
                    }
                })
            }
        } 
    }
    

    【讨论】:

      猜你喜欢
      • 2020-01-21
      • 1970-01-01
      • 2011-10-17
      • 2018-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多