248次后AVAudioPlayer无法正常工作 [英] AVAudioPlayer doesn't work after 248 times

查看:98
本文介绍了248次后AVAudioPlayer无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个涉及反复单击按钮的应用程序,并为此点击(点击波形文件)提供音频反馈.

I am developing an application that involves clicking a button repeatedly, and I am giving an audio feedback for this click, a click wave file.

它工作正常,但是我有一个问题,正好是248次之后,声音不再播放了,但是应用程序没有崩溃.我收到以下错误:

it works fine, but I have a problem, after exactly 248 times, the sound is not played any more, the app doesn't crash however. and I get the following error:

错误域= NSOSStatusErrorDomain代码= -43操作无法完成.(NSOSStatusErrorDomain错误-43.)"

Error Domain=NSOSStatusErrorDomain Code=-43 "The operation couldn't be completed. (NSOSStatusErrorDomain error -43.)"

这是我使用的代码(相当标准)

here is the code I am using, (pretty standard)

//file.h
AVAudioPlayer *audioPlayer;


//file.m
    NSString *path=[[NSBundle mainBundle] pathForResource:@"click" ofType:@"wav"];

        [audioPlayer stop]; //I added this in one of my trials to solve it.
        audioPlayer= [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path]  error:&err];
        audioPlayer.numberOfLoops=0;
        audioPlayer.delegate=self;
           [audioPlayer prepareToPlay];



        if (audioPlayer==nil)
            NSLog(@"%@, %@",path,[err description]);
        else
        [audioPlayer play];

知道为什么会这样吗?

推荐答案

我只见过与流音频相关的错误消息. NSURL或...

I've only ever seen that error message associated with streaming audio. Might be something strange happening with the NSURL or...

代码的其他问题:什么时候释放audioPlayer对象?否则,您将分配许多对象,并且永远不会从内存中释放它们.最终可能导致内存不足消息.

Other issue with your code: When do you release the audioPlayer object? If you aren't, you're allocating a lot of objects and never releasing them from memory. That could eventually lead to a low memory message.

另一个问题:

Another issue: Apple recommends using an IMA4 encoded *.caf file for playing multiple sounds at once

最后,我从另一个问题中发布了一个示例项目,该示例项目可能显示了使用AVAudioPlayer的更好方法...请参阅此帖子

Lastly, I have posted a sample project from another question which might show a better way of using AVAudioPlayer ...See this post what is the best way to play sound quickly upon fast button presses xcode?

这篇关于248次后AVAudioPlayer无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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