初始化AVAudioPlayer对象时如何处理来自NSOSStatusErrorDomain的错误代码-43? [英] How to handle error code -43 from NSOSStatusErrorDomain when initializing AVAudioPlayer Object?

查看:111
本文介绍了初始化AVAudioPlayer对象时如何处理来自NSOSStatusErrorDomain的错误代码-43?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用AVAudioPlayer时观察到了奇怪的行为

I observed strange behavior while working with AVAudioPlayer

以下是代码:

AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@",fileName]] error: &error];

在这种情况下,我从服务器下载了文件并将其存储在应用程序的Cache目录中.

In this, I downloaded the file from server and stored in application's Cache directory.

我遇到以下错误:

Error in playing =
Domain = NSOSStatusErrorDomain
Code = -43
Description = Error Domain=NSOSStatusErrorDomain Code=-43 "The operation couldn’t be completed. (OSStatus error -43.)"

我还验证了该位置存在该文件. 每次重新启动应用程序时,我在播放歌曲时都会遇到相同的错误. 一段时间后,当我尝试运行相同的代码时,播放器运行正常,没有任何错误.

I also verified that file is present at that location. Everytime I restart my application, I was getting same error for song play. After some time, when I tried to run same code, my player just works fine without any error.

有人可以告诉我如何处理此错误吗?

Can anyone tell me how to handle this error?

还,有人可以向我解释一下问题是什么吗?

Also, Can anyone explain me what was the problem?

推荐答案

即使我可以验证songCacheURL是否有效且文件可用,此代码也存在相同的错误:

I had the same error with this code, even though I could verify that the songCacheURL was valid and that the file was available:

self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:songCacheURL error:&error];

我能够解决该问题,方法是先将文件加载到NSData元素中,然后使用它来初始化AVAudioPlayer,如下所示:

I was able to fix the issue by loading the file first into an NSData element and then using that to initialize the AVAudioPlayer instead, like so:

NSData *songFile = [[NSData alloc] initWithContentsOfURL:songCacheURL options:NSDataReadingMappedIfSafe error:&error1 ];
self.audioPlayer = [[AVAudioPlayer alloc] initWithData:songFile error:&error2];

我希望对其他人有帮助.

I hope that helps someone else.

罗宾

这篇关于初始化AVAudioPlayer对象时如何处理来自NSOSStatusErrorDomain的错误代码-43?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆