AVAudioPlayer initWithURL无法正常流式传输

时间:2013-07-25 11:49:28

标签: ios url avaudioplayer radio

我正在尝试在我的应用中播放广播。

我正在使用AVAudioPlayer,但我有一个我不明白的错误:

NSString *urlString = @"http://broadcast.infomaniak.net/radionova-high.mp3";
NSURL *myURL = [NSURL URLWithString:urlString];

NSError *error;
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:myURL error:&error];

if (self.audioPlayer == nil){
    NSLog(@"Error loading clip: %@", [error localizedDescription]);
}
else {
    self.audioPlayer.delegate = self;
    self.audioPlayer.volume = 0.5;
    [self.audioPlayer play];
}

我有以下错误:

Error loading clip: The operation couldn’t be completed. (OSStatus error -43.)

有人看到了什么问题吗?

编辑:

这就是Apple文档所说的:

  

AVAudioPlayer类不支持流式音频   基于HTTP URL。与initWithContentsOfURL一起使用的URL必须是   文件URL(文件://)。也就是说,本地路径。

现在怎样? :(

1 个答案:

答案 0 :(得分:0)

将文件下载到文档目录然后播放文件

NSString *urlString = @"http://broadcast.infomaniak.net/radionova-high.mp3";
NSURL *myURL = [NSURL URLWithString:urlString];
    NSData *audioData = [NSData dataWithContentsOfURL:myURL];
        NSString *docDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        NSString *filePath = [NSString stringWithFormat:@"%@/%@.mp3", docDirPath , fileName];
        [audioData writeToFile:filePath atomically:YES];

        NSError *error;
        NSURL *fileURL = [NSURL fileURLWithPath:filePath];
        self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&error];
        if (self.audioPlayer == nil) {
            NSLog(@"AudioPlayer did not load properly: %@", [error description]);
        } else {
       self.audioPlayer.delegate = self;
      self.audioPlayer.volume = 0.5;
            [self.audioPlayer play];
        }