Stream data from network in AVAudioEngine, is it possible?

2.2k views Asked by At

I have an app in which I use AVAudioEngine for playing files from the local file system by using AVAudioPlayerNodes and AVAudioFiles. This works perfectly fine.

Now I would like to enable my setup to also support streaming of MP3 files from a server on the internet.

What I've tried so far

My hope was that I could create some sort of buffers from NSURL objects pointing to network addresses, which I could then use with my AVAudioPlayerNode.

I've searched Stack Overflow and the internet in general but haven't found any good tips on how to achieve this.

I know that the AVAudioEngine lineup consists of:

  • AVAudioPlayerNode, which can play audio from an AVAudioFile or a AVAudioBuffer. I already use AVAudioFile for my "local file setup" today.
  • AVAudioFile, which is to be used for local files. An 'AVAudioFile' is created with an 'NSURL' so I've tried with an URL that points to a MP3 file on a server like so in a Playground:

    var fileError: NSError? = nil
    let file = AVAudioFile(forReading: someURL, error: &fileError)
    if fileError != nil {
        fileError
    }
    

which rewards me with this error:

Error Domain=com.apple.coreaudio.avfaudio Code=2003334207 "The operation couldn’t be completed. (com.apple.coreaudio.avfaudio error 2003334207.)" UserInfo=0x7fbfab424480 {failed call=ExtAudioFileOpenURL((CFURLRef)fileURL, &_extAudioFile)}
  • AVAudioPCMBuffer, which is the class I was hoping to use, but I can not find any way to instantiate it with data.

My question (just to make it clear :-))

Does any of you bright people out there know how to use AVAudioEngine for streaming MP3 files from a server on the internet?

Or should I just give up and use AVPlayer for this?

Looking forward to hearing from you.

1

There are 1 answers

1
Oren On BEST ANSWER

Apple's documentation doesn't specify if it can work with a mp3 hosted online but as soon as I try to initialize an AVAudioFile with a mp3 url it crashes instantly with a fatal exception. I think that answers your question.