gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 05:53:53 24 4
gpt4 key购买 nike

我在我的应用程序中使用 AVPlayer 进行实时流式传输并且工作正常。

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

AVPlayerrate 属性自动为零。

我认为线程可能存在一些问题,但我不明白。

我的代码如下

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], context: nil)
myPlayer.currentItem?.addObserver(self, 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 & failed 信息。

最佳答案

尝试在主线程上播放流。

DispatchQueue.main.async { [unowned self]
self.play_Live_Stream()
}

关于ios - 下载文件导致在 ios 中流式传输的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39996094/

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