我无法播放 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/