views:

74

answers:

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];
}
+2  A: 

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

tadej5553
And change [NSURL fileURLWithPath:path] to [NSURL URLWithString:path]
tadej5553
A: 

Thank you very mush , i changed it to :

-(IBAction)playSound {

NSString *path = @"http://www.domain.com/audio/test.mp3";
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:path] error:NULL];
                        theAudio.delegate = self;
                         [theAudio play];

}

but there are error ! warning: class 'SoundtestViewController' does not implement the 'AVAudioPlayerDelegate' protocol

aldebasi
You have to implement the AVAudioPlayerDelegate protocol in your SoundtestViewController (self). This is another problem.I thinkn that this is only a warning and not an error, so if you have implemented the delegate methods, you shouldn't e bothered by this
tadej5553
tadej5553 thank you very much , i deleted test.mp3 from my project and now it's build and run without error . but the problem now is when i press the button the file not play !!its still silent ! i appreciate you help :)
aldebasi