3

I need to create audio player. I get audio from url and i download audio from url then i play it. But i need play audio while audio downloading how i can do this?

This is my code how play it from 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)
}
beka.angsabay
  • 413
  • 1
  • 6
  • 15

1 Answers1

5

AVAudioPlayer can not - straight away - stream content from a remote URL.

As it says in the documentation for AVAudioPlayer

Apple recommends that you use this class for audio playback unless you are playing audio captured from a network stream or require very low I/O latency.

See also this thread for instance, or this answer.

What you can do if you would like to stream from a remote URL, is use the AVPlayer instead of the AVAudioPlayer. Documentation can be found here

To create a player capable of streaming you'd do something along these lines.

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

Hope that helps you.

Community
  • 1
  • 1
pbodsk
  • 6,787
  • 3
  • 21
  • 51
  • how i handle streaming procession, like showing in slider streaming – beka.angsabay Sep 11 '16 at 10:10
  • 1
    I don't have any experience with that sorry, but maybe you could look into `AVPlayerViewController` (https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVPlayerViewController_Class/) which will give you controls, including a slider for progress. Otherwise I'm sure someone else has had that problem too, so you should be able to find a solution. Try googling for `AVPlayer audio streaming controls` for instance. – pbodsk Sep 11 '16 at 10:42
  • When simply implementing `player.play()`, does it need to download the whole file first before playing it? or it will automatically do the right job? – Ahmad F Aug 17 '17 at 07:31