开发者

iPhone : Looping Audio stops after 4 mins

I am making a count down timer which plays a "beep" sound for every second counting through an NSTimer. The app must be awake for a max period of 90min and play the repeating beep sound until the timer is invalidated. From the plist i selected "UIbackground Modes App plays audio" and also added to the delegate.m [UIApplication sharedApplication].idleTimerDisabled = YES; in order to keep my timer on screen. The problem is that after 4 minutes the repeating sound stops. Everything else is working, the app seems awake, the label showing the countdown updates itself. In order to restore audio you have to quit and re-launch the app. The problem occurs in both the simulator and the device (iPhone 3G). I am using xcode 4 if that matters. Here is my code:

NSString *path = [[NSBundle mainBundle]
                   pathForResource:@"beep" ofType:@"mp3"];
 AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:nil];
 [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
 UIBackgroundTaskIdentifier bgTask = [[UIApplication sharedApplication]beginBackgroundTaskWithExpirationHandler:nil];
 [[UIApplication sharedApplication] endBackgroundTask:bgTask];
 [audioPlayer play];

Of course, i have imported the libraries

#import <AVFoundation/A开发者_如何学编程VFoundation.h>
#import <AudioToolbox/AudioToolbox.h>

p.s. I am reading for hours topics about background audio and tested nearly everything i could find but nothing worked for me. If somebody can help me, please have in mind that i am very new to programming. Thanks in advance.


From the posted code it looks like you create a new AVAudioPlayer object every time you play the sound and don't release it later. That causes your file (beep.mp3) to stay opened and if too many files are opened, you'll run out of file handlers. If that's the case, than you can't open any more files, which can cause the silence you experiencing.

If you releasing/reusing everything properly, than it's probably can't be the case.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜