gpt4 book ai didi

ios - 来自服务器的 MP3 文件无法在 iOS7 中使用 AVAudioPlayer 播放

转载 作者:行者123 更新时间:2023-11-29 02:10:12 26 4
gpt4 key购买 nike

这是我的代码,首先我调用 mp3 url 来下载音频数据。然后我将该数据保存在本地目录中,之后我从本地文件路径播放音频但它不工作 AVAudioPlayer 抛出错误。我也尝试过其他一些方法,但没有人适合我。在模拟器上相同的代码运行良好。音频 url 在浏览器中播放得非常棒。

 NSURL *urlll=[NSURL URLWithString:@"www.xyz.audio.mp3"];

[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:urlll] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *responce,NSData* data,NSError * error) {
if (error == nil)
{
indicator.hidden=YES;
[indicator stopAnimating];

if (data)
{
NSString *docDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/%@.mp3", docDirPath , @"Audio"];
[data writeToFile:filePath atomically:YES];

NSURL *fileURL = [NSURL fileURLWithPath:filePath];
AudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&error];

AudioPlayer.delegate=self;
[AudioPlayer prepareToPlay];
if (AudioPlayer == nil) {
NSLog(@"AudioPlayer did not load properly: %@", [error description]);
} else {
[AudioPlayer play];

}
}
}
}];

但我遇到了这些错误:Error Domain=NSOSStatusErrorDomain Code=1685348671 "The operation couldn't be completed. (OSStatus error 1685348671.)

请帮助我:( .

最佳答案

在您的代码中,您将音频保存到 NSString *filePath,但您使用 fileURL 启动。您是否真的想这样做:

AudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:filePath] error:&error];

如果不是,那么您应该知道该错误正在向您报告 kAudioFileInvalidFileError。这意味着文件格式错误。您可以尝试其他格式,例如 .caf 或 .aifcaiff

--编辑--

确保 AVAudioPlayer 是 .m 或 .h 文件中的属性,以便内存分配在离开该方法后继续:

@property (nonatomic,strong) AVAudioPlayer *AudioPlayer;

(旁注:属性应以小写开头 -> audioPlayer)

关于ios - 来自服务器的 MP3 文件无法在 iOS7 中使用 AVAudioPlayer 播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29367801/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com