问题
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];
}
回答1:
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
回答2:
try this:
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:path]];
AVAudioPlayer *audio = [[AVAudioPlayer alloc] initWithData:data error:nil];
[audio play];
来源:https://stackoverflow.com/questions/3273197/i-want-to-play-mp3-files-on-my-server-with-avaudioplayer