I want to play mp3 files on my server with AVAudioPlayer

会有一股神秘感。 提交于 2019-12-24 00:44:23

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!