ios - 如何使用 AVAudioPlayer 播放 'http:/......./mp3'

标签 ios objective-c avaudioplayer

我无法播放 mp3 音频文件。

URL_PATH = @"http://...../.…./A1.mp3";

NSError *错误;
player = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL URLWithString:URL_PATH] error:&error];
if (error) 
{
    NSLog(@"error:%@",error.localizedDescription);
}

if (player ==nil)
{
    NSLog(@"nil");
}
else 
{
    NSLog(@"playing");
    [player play];
}  

错误代码 :
error:The operation couldn’t be completed. (OSStatus error -43.)
nil

我能怎么做?
请告诉我提示。预谢谢!

最佳答案

您使用错误的播放器播放从网络流中捕获的音频。参见 Apple 关于 AVAudioPlayer 的文档:

Apple recommends that you use this class for audio playback unless you are playing audio captured from a network stream or require very low I/O latency. For an overview of audio technologies, see Audio & Video Starting Point and “Using Audio” in Multimedia Programming Guide.



一种使用 AVAudioPlayer 的解决方法是获取音频的数据并将其放入 NSData .然后你可以初始化你的AVAudioPlayer通过 initWithData:error: .另外,不要将代码放在主队列中。否则它将阻止您的用户界面对用户的输入使用react。
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    NSURL *mp3URL = [NSURL URLWithString:@"http://zhangmenshiting.baidu.com/data2/music/44799433/4479927746800128.mp3?xcode=dbff90a141d5d136fdc7275fdc3fae126077a44adc974ad8"];
    NSData *data = [NSData dataWithContentsOfURL:mp3URL];
    self.audioPlayer = [[AVAudioPlayer alloc] initWithData:data error:NULL];
    [self.audioPlayer play];
});

关于ios - 如何使用 AVAudioPlayer 播放 'http:/......./mp3',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16975115/

相关文章:

ios - iOS (iPhoneX) 上的 html5 视频播放

ios - CoreData 中具有多个上下文的并发性

android - 如何在 react-native 中为 webView 请求设置自定义 header

ios - 如何使用对象数组填充 TableView?

iphone - 使用 AVAudioPlayer 的音频流

iphone - 为 UITextView 添加轮廓/描边效果 ios <7

ios - iOS-在“NSArray”类型的对象上找不到属性“名称”

objective-c - 具有动态显示图像的大量 ImageView

swift - Mac 上的 AVAudioPlayer 无法播放/崩溃

ios - 同时录制和播放音频的问题