简体   繁体   中英

How to stream mp3 files (iOS)

I have found many related questions on SO, but or because I am very new, or because it doesn't exactly fit my problem, I can't find a way to succeed.

So, all I would like to do is playing an audio file stored on an exernal server directly on the iOS device (iPhone in my case)

I have tried differents solution on the net, and this is what I get until now.

NSString* resourcePath = @"http://www.chiptape.com/chiptape/sounds/medium/laugh.mp3"; //your url
NSURL *url = [[NSURL alloc]initWithString:resourcePath];

NSError *error;

audioPlayer = [[AVAudioPlayer alloc] initWithURL:url error:&error];

audioPlayer.delegate = self;
[url release];

[audioPlayer prepareToPlay];
[audioPlayer play];

The compiler show me this error:

No visible @interface for 'AVAudioPlayer' declares the selector 'initWithURL:error:'

I am not sure at all about my code, and would like your guys to know I have started iOS coding yesterday, so it's not impossible I'm missing something so obvious that tutorials/posts don't mention it.

Also, I have imported the library AVFoundation and imported it on my .h file. I have also declared this on my .h file

AVAudioPlayer *audioPlayer;

I hope there is an easy way to stream audio file on iOS as provide android/java

Thank you and if you need more details comment and I'll update my post.

I'm working with it recently.

AVAudioPlayer can't work with audio stream. In fact, there isn't an easy way to do that.

You need to learn some Core Audio things, get the raw data of media, play them in audioQueue (audioToolBox framework). Real-time audio processing is not a simple work, it's full of c-level api and so many dirty work, don't rush, have fun :)

Check out the book Learning Core Audio and this open-source audio streamer .

Let me know if you need more help.

Try this:

NSString* resourcePath = @"http://www.chiptape.com/chiptape/sounds/medium/laugh.mp3"; //your url
NSURL *audioURL = [NSURL URLWithString:resourcePath];
AVAudioPlayer *audioPlayer = [[[AVAudioPlayer alloc] initWithContentsOfURL: audioURL  error:&error]autorelease];       
[audioPlayer play];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM