【问题标题】:Download file cause issue for streaming in ios下载文件导致在 ios 中流式传输的问题
【发布时间】:2017-02-21 02:34:47
【问题描述】:

我在我的应用中使用带有 AVPlayer 的实时流媒体并且工作正常。

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

AVPlayerrate 属性自动为零。

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

My Code is as Follow

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")
        }
    }
    
}

但它没有给我任何关于为什么流停止的信息。 如果有的话,我还会观察到停滞和失败的信息。

【问题讨论】:

  • 你能展示一下play_Live_Stream的实现吗?
  • @RhythmicFistman,我添加了播放流的代码。
  • 您一次下载多少个文件?您可以尝试一次下载一个,而不是一次开始所有下载吗?也许你正在使你的连接饱和。你观察到playbackLikelyToKeepUp 有什么变化吗?
  • @RhythmicFistman,我现在正在下载大约 5 个文件。我没有对playbackLikelyToKeepUp 进行任何更改。 但我正在将 rate 更改为 0 ,因此播放器会自动暂停,即使下载完成也不会再次恢复 也许你是对的,让我尝试一次下载一个。
  • @user2526811 可以尝试延迟播放直播吗?

标签: ios swift alamofire avplayer


【解决方案1】:

尝试在主线程播放流。

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

【讨论】:

    【解决方案2】:

    尝试在主线程上播放流。

    DispatchQueue.main.async { 
    for int i in arr_favourites{
         Alamofire.download(urlString, to: destination).response { response in
             print(response)
         }
       }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-26
      • 2012-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多