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

时间:2016-10-12 10:15:45

标签: ios iphone swift alamofire avplayer

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

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

rate的{​​{1}}属性自动为零。

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

我的代码如下

AVPlayer

尝试2(默认NSURLSession)

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

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

2 个答案:

答案 0 :(得分:0)

尝试在主线程上播放流。

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

答案 1 :(得分:0)

尝试在主线程上播放流。

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