2013-03-19 65 views
0

我正在播放来自服务器的音频文件,但它不播放如果我播放本地文件,那么它在这里播放是我的代码。音频文件不能从服务器上播放

//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]; 

回答

0

尝试使用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]; 
+0

它给在片源类型的错误 – 2013-03-19 10:12:08

0

使用此代码为您解决问题...

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];