Iphone 无法从服务器播放音频文件
我正在播放服务器上的音频文件,但如果我播放本地文件,它就不会播放。这里播放的是我的代码Iphone 无法从服务器播放音频文件,iphone,objective-c,ipad,avaudioplayer,Iphone,Objective C,Ipad,Avaudioplayer,我正在播放服务器上的音频文件,但如果我播放本地文件,它就不会播放。这里播放的是我的代码 //NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/audiofile.mp3", [[NSBundle mainBundle] resourcePath]]]; NSURL *url = [NSURL URLWithString:@"http://celeritas-solutions.com/pah_brd_
//NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/audiofile.mp3", [[NSBundle mainBundle] resourcePath]]];
NSURL *url = [NSURL URLWithString:@"http://celeritas-solutions.com/pah_brd_v1/productivo/1.mp3"];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;
if (audioPlayer == nil)
NSLog([error description]);
else
[audioPlayer play];
使用此代码解决您的问题
NSData *_objectData = [NSData dataWithContentsOfURL:[NSURL URLWithString:http://celeritas-solutions.com/pah_brd_v1/productivo/1.mp3]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithData:_objectData error:&error];
audioPlayer.numberOfLoops = 0;
audioPlayer.volume = 1.0f;
[audioPlayer prepareToPlay];
if (audioPlayer == nil)
NSLog([error description]);
else
[audioPlayer play];
尝试使用
MPMoviePlayerController
而不是AVAudioPlayer
NSURL *url = [NSURL URLWithString:@"http://celeritas-solutions.com/pah_brd_v1/productivo/1.mp3"];
mv = [[MPMoviePlayerController alloc] initWithContentURL:url];
mv.movieSourceType = MPMovieSourceTypeUnknown;
[self.view addSubview:mv.view];
[mv play];