0

我正在尝试在我的应用程序中播放收音机。

我正在使用 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 (file://)。即,本地路径。

怎么办 ?:(

4

1 回答 1

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];
        }
于 2013-07-25T11:57:41.620 回答