2

I want to play mp3 files on my server with AVAudioPlayer I've tried this code, but it does not work.

-(IBAction)playSound {  
    NSString *path = [[NSBundle mainBundle] pathForResource:@"http://www.domain.com/audio/test.mp3"];
    AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    theAudio.delegate = self;
    [theAudio play];
}
Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
aldebasi
  • 21
  • 3
  • 1
    Welcome to stackoverflow! I took the liberty of reformatting your code. You may want to tell us more about your problem, especially why it doesn't work. What error message do you get? At what spec (configuring, compilation or execution) does the error occur? Why do you think your example should work? – phihag Jul 17 '10 at 20:49

2 Answers2

2

NSBundle can only select the files in the apps bundle, not the ones on the internet.

Make this the path

NSString *path = @"http://www.domain.com/audio/test.mp3";

and leave everything else the same and it should work

phihag
  • 278,196
  • 72
  • 453
  • 469
tadejsv
  • 2,085
  • 1
  • 18
  • 20
0

try this:

NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:path]];
AVAudioPlayer *audio = [[AVAudioPlayer alloc] initWithData:data error:nil];
[audio play];
Hsm
  • 1,510
  • 17
  • 16