2016-09-10 2 views
3

오디오 플레이어를 만들어야합니다. URL에서 오디오를 가져오고 URL에서 오디오를 다운로드 한 다음 재생합니다. 하지만 오디오를 재생하면서 오디오를 재생해야하는 이유는 무엇입니까?URL에서 다운로드하는 동안 오디오를 재생하는 방법은 무엇입니까?

이것은 URL에서 그것을 연주하는 내 코드입니다 :

func URLSession(session: NSURLSession, 
       downloadTask: NSURLSessionDownloadTask, 
       didFinishDownloadingToURL location: NSURL){ 
    do { 
     loadingView.hidden=true 
     actInd.stopAnimating() 
     UIApplication.sharedApplication().networkActivityIndicatorVisible = false 
     player=try AVAudioPlayer(contentsOfURL: location) 
     player?.delegate=self 
     selectedAudio.status=true 
     selectedAudio.isDownload=true 
     player?.enableRate=true 
     switch speedType_Index { 
     case 0: 
      appDelegate.player?.rate=Float(1) 
      break 
     case 1: 
      appDelegate.player?.rate=Float(1.5) 
      break 
     case 2: 
      appDelegate.player?.rate=Float(2) 
      break 
     case 3: 
      appDelegate.player?.rate=Float(0.5) 
      break 
     default: 
      break 
     } 
     switch playingType_Index { 
     case 0: 
      appDelegate.player?.numberOfLoops = 0 
      break 
     case 1: 
      appDelegate.player?.numberOfLoops = -1 
      break 
     default: 
      break 
     } 
     player?.volume=Float(volume) 
     player?.play() 
     self.tableView.reloadData() 

    }catch let error as NSError{ 
     print(error.localizedDescription) 
    } 
} 

func URLSession(session: NSURLSession, 
       downloadTask: NSURLSessionDownloadTask, 
       didWriteData bytesWritten: Int64, 
          totalBytesWritten: Int64, 
          totalBytesExpectedToWrite: Int64){ 
    let progress=Float(totalBytesWritten)/Float(totalBytesExpectedToWrite); 
    bite.text=String(format: "%.1f%%",progress * 100) 
} 

답변

2

AVAudioPlayer 수 없습니다 - 바로 - 원격 URL에서 스트림 콘텐츠입니다. 그것은 documentation for AVAudioPlayer

에서 말했듯이

애플은 네트워크 스트림에서 캡처 한 오디오를 재생하거나 매우 낮은 I/O 대기 시간을 필요로하지 않는 오디오 재생 이 클래스를 사용하는 것이 좋습니다.

예를 들어 this thread 또는 this answer도 참조하십시오.

원격 URL에서 스트리밍하려는 경우 수행 할 수있는 작업은 AVAudioPlayer 대신 AVPlayer을 사용하는 것입니다. 문서를 찾을 수 있습니다 here

스트리밍 할 수있는 플레이어를 만들려면이 줄을 따라 뭔가를 할 수 있습니다.

var player = AVPlayer() //declared as a property on your class 

if let url = NSURL(string: "https://archive.org/download/testmp3testfile/mpthreetest.mp3") { 
    player = AVPlayer(URL: url) 
    player.volume = 1.0 
    player.play() 
} 

희망이 당신을 돕습니다.

+0

슬라이더 스트리밍에서 보여주는 것처럼 스트리밍 행렬을 처리하는 방법 –

+1

죄송합니다. 경험이 없지만 'AVPlayerViewController' (https://developer.apple.com/library/ios/)를 살펴볼 수는 있습니다. documentation/AVFoundation/Reference/AVPlayerViewController_Class /)를 사용하면 진행 상황에 대한 슬라이더를 비롯하여 컨트롤을 제공합니다. 그렇지 않으면 다른 사람도 그 문제를 겪었 기 때문에 해결책을 찾을 수 있어야합니다. 예를 들어 'AVPlayer 오디오 스트리밍 컨트롤'에 대한 인터넷 검색을 시도해보십시오. – pbodsk

+0

도움을 주셔서 감사합니다 –

관련 문제