AVAudioPlayer doesn't work after 248 times

本小妞迷上赌 提交于 2019-12-10 11:35:59

问题


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.

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:

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];

any idea why this happens?


回答1:


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

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

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?



来源:https://stackoverflow.com/questions/3401069/avaudioplayer-doesnt-work-after-248-times

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!