views:

182

answers:

3

I want to play an audio file which is posted on a website. I'm trying to use AVAudioPlayer and set the url to its website link. But the bgPlayer turns out to be nil. I think I can download this audio file and play it afterwards, but is there any way to play it while I'm downloading it?

NSURL *bgURL = [NSURL URLWithString:@"http://www.radioslots.com/iphone/test/tmp/1.mp3"];
bgPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:bgURL error:nil];
bgPlayer.numberOfLoops = -1;
if (bgPlayer) {
    [bgPlayer play];
}
+1  A: 

I have just written answer here.Check it and replace the url used there with yours..

you just replace

   NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"songname" ofType:@"mp3"];
 NSURL *newURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];

with

NSString *soundFilePath = @"Your URL";
NSURL *newURL = [[NSURL alloc] initWithString: soundFilePath];
Nithin
But I want to play an audio file on the website, not from a local file.
iPhoney
A: 

There is a proper documentation available at this link

But I think you should also check the error code you are getting by this way:

NSError *error;
bgPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:bgURL error:&error];

Hope this helps.

Madhup
A: 

AVAudioPlayer isn't made to open network streams, I could be mistaken but I'm pretty sure it's not possible.

Just off the top of my head, I can think of two ways that would probably work: 1. Use MPMoviePlayer, as this can open network streams and despite the name works with audio. The only thing is this pops up the modal player with controls. 2. Download the network file and store it locally, then use AVAudioPlayer to play the local file.

ACBurk
Thanks. MPMoviePlayer can play mp3 on websites just like you mentioned.
iPhoney