音频播放器工作正常,如果我启动它20到30次,但之后它显示以下错误
错误错误代码=-43“无法完成操作。(OSStatus Domain=NSOSStatusErrorDomain -43。)”
我目前使用的代码如下
- (void)playShortSound:(NSString *)fileName type:(NSString *)type
{
if (!isSound) {
return;
}
NSString *soundFilePath =[[NSBundle mainBundle] pathForResource: fileName ofType: type];
NSURL *fileURL = [[[NSURL alloc] initFileURLWithPath: soundFilePath] autorelease];
NSError *error;
AVAudioPlayer *newPlayer =[[AVAudioPlayer alloc] initWithContentsOfURL: fileURL
error: &error];
//newPlayer.numberOfLoops = 0;
newPlayer.volume=1.0;
if (newPlayer == nil){
NSLog(@"%@",[error description]);
//[self playShortSound:fileName type:type];
//return;
}
else{
[newPlayer play];
newPlayer.delegate=self;
}
}
- (void) audioPlayerDidFinishPlaying: (AVAudioPlayer *) player
successfully: (BOOL) completed {
if (completed == YES) {
NSLog(@"Sound Playing complete");
}else {
NSLog(@"error in completion of %@",player.url);
}
}发布于 2013-02-16 11:15:44
我也遇到了同样的问题,我没有从url初始化播放器,而是从NSData初始化。
NSString *soundFilePath =[[NSBundle mainBundle] pathForResource: fileName ofType: type];
NSData *soundData = [[NSData alloc] initWithContentsOfFile:soundFilePath];
NSError *error;
AVAudioPlayer *newPlayer =[[AVAudioPlayer alloc] initWithData: soundData
error: &error];它似乎解决了我的问题,希望它也能解决你的问题。
发布于 2011-03-17 14:16:21
通过多次调用该方法,您将多次分配AVAudioPlayer newPlayer的实例。这肯定会因为分配的内存而给您带来问题。只要确保你使用完你拥有的对象后,你就可以release它们了。
发布于 2013-07-29 11:14:30
你设置AudioSessionCategory了吗?
{NSError *setCategoryError = nil;
BOOL success = [[AVAudioSession sharedInstance] setCategory: avaudiosessioncategoryplayandrecorderror: &setCategoryError];
}https://stackoverflow.com/questions/5335431
复制相似问题