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

2 回答 2

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];
于 2013-03-19T09:38:59.223 回答
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];
于 2013-03-19T09:48:51.397 回答