下载文件导致ios中的流式传输问题

我在我的应用程序中使用AVPlayer实时流式传输工作良好.

但是当我尝试使用Alamofire.downlaod方法下载某个文件时,流停止工作.

AVPlayer的rate属性自动为零.

我认为线程可能存在一些问题,但我没有得到它.

我的守则如下

override func viewDidLoad() {

 play_Live_Stream()
}


override func viewWillAppear() {

 Download_favourite_files()
}

func  Download_favourite_files() {
     for int i in arr_favourites{
           Alamofire.download(urlString,to: destination).response { response in
                        print(response)
          }
      }

    }

func play_Live_Stream(){
myplayerItem = AVPlayerItem(url: url as! URL)
        myPlayer = AVPlayer(playerItem: myplayerItem)
        myPlayer.volume = volume_slider.value
        PlayerObserving = true
        NotificationCenter.default.addobserver(self,selector: #selector(playInstalled(noti:)),name: NSNotification.Name.AVPlayerItemPlaybackStalled,object: myPlayer.currentItem)
        NotificationCenter.default.addobserver(self,selector: #selector(playFailed(noti:)),name: NSNotification.Name.AVPlayerItemFailedToPlayToEndTime,object: myPlayer.currentItem)
        myPlayer.currentItem?.addobserver(self,forKeyPath: "status",options: NSkeyvalueObservingOptions.new,context: nil)
        myPlayer.currentItem?.addobserver(self,forKeyPath: "timedMetadata",options: [.new,.old,.initial],forKeyPath: "playbackLikelyToKeepUp",options: .new,context: nil)

}

override func observeValue(forKeyPath keyPath: String?,of object: Any?,change: [NSkeyvalueChangeKey : Any]?,context: UnsafeMutableRawPointer?) {

        if ((object! as AnyObject).isEqual(myPlayer.currentItem) && keyPath=="status"){
            if myPlayer.status == AVPlayerStatus.readyToPlay{
                print("ReadyToPlay")

                myPlayer.play()
            }
    }

尝试2(认NSURLSession)

func load_downloads(){
for track in arr_tracks{

                let urlString = BASE_URL + "/track/download/" + "\(track.id)"
                print("downloaded url",urlString)
                let url = NSURL(string: urlString)
                let downloadTask = backgroundSession.downloadTask(with: url as! URL)
                downloadTask.resume()
            }
        }


func urlSession(_ session: URLSession,downloadTask: URLSessionDownloadTask,didFinishDownloadingTo location: URL) {

        let documentsURL = FileManager.default.urls(for: .documentDirectory,in: .userDomainMask)[0]
        let destinationURLForFile = documentsURL.appendingPathComponent((downloadTask.originalRequest?.url?.lastPathComponent)! + "_" + (downloadTask.response?.suggestedFilename)!)

        let fileManager = FileManager()

        if fileManager.fileExists(atPath: destinationURLForFile.path){
        }
        else{
            do {
                try fileManager.moveItem(at: location as URL,to: destinationURLForFile as URL)
            }catch{
                print("An error occurred while moving file to destination url")
            }
        }

    }

但它没有给我任何关于为什么流停止的信息.
我也观察到Stalled& amp;失败信息,如果有的话

解决方法

尝试在主线程上播放流.
dispatchQueue.main.async { [uNowned self]
 self.play_Live_Stream()
}

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...