2016-10-12 1 views
8

내 앱에서 AVPlayer로 라이브 스트리밍을 사용하고 있습니다. &이 정상적으로 작동합니다.ios에서 스트리밍에 대한 파일 다운로드 문제가 발생했습니다.

그러나 Alamofire.downlaod method을 사용하여 파일을 다운로드하려고하면 스트림이 작동하지 않습니다.

rate의 속성은 AVPlayer입니다. 자동으로 입니다.

스레딩에 문제가있을 수 있지만 문제가되지는 않을 것이라고 생각합니다. 그것은 나에게 스트림이 중단 된 이유에 대한 정보를 제공하지 않습니다하지만

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

    } 

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

TRY 2 (기본 NSURLSession)

팔로우

내 코드입니다. 나는 또한 지연된 정보가있는 경우 & 정보가 실종 된 것을 관찰합니다.

+0

'play_Live_Stream' 구현을 표시 할 수 있습니까? –

+0

@RhythmicFistman, 스트림 재생 코드를 추가했습니다. – user2526811

+0

한 번에 얼마나 많은 파일을 다운로드합니까? 모든 다운로드를 한 번에 시작하는 대신 한 번에 하나씩 다운로드 할 수 있습니까? 어쩌면 당신은 당신의 연결을 포화시키고있을 것입니다. 'playbackLikelyToKeepUp'에 대한 변경 사항을 준수합니까? –

답변

0

메인 스트림에서 스트림을 재생 해 봅니다.

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

답변을 주셔서 감사합니다.하지만 그걸 시도했지만 여전히 작동하지 않습니다 ... – user2526811

0

스트림을 메인 스레드에서 재생 해 봅니다.

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

나는 이전 답변으로 시도했지만 여전히 작동하지 않습니다! – user2526811

관련 문제