Reputation: 367
I am working on an alarm application, I need to find the best method of playing a sound that will:
My main requirements are my first two points, have tried using SystemSounds however I can't seem to stop these, once you tell it to play it plays until the sound is finished. This however works in the background but cannot be looped. And is silenced by the mute switch.
Am using an AudioSession currently to ignore the mute switch. Am also currently using AVAudioPlayer which allows me to control the play/pausing of the sound. But won't work in the background! Is there any way of using AVAudioPlayer and AudioSession to play a background sound or do I need to use something else? OpenAL?
Any help will be GREATLY appreciated...
Thanks,
Paul
Upvotes: 1
Views: 1592
Reputation: 367
Okay I looked into using AudioServices and have used the following code:
// Use SystemSound API's
NSURL *alarmSound = [[NSBundle mainBundle] URLForResource:alarm.alarmTone withExtension:@"wav"];
soundFileURLRef = (CFURLRef) [alarmSound retain];
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundFileObject);
AudioServicesPlayAlertSound(soundFileObject);
[alarmSound release];
This plays sounds fine in the background, and AudioServicesPlayAlertSound also triggers a vibration too which is a bonus. Although you can not actually stop the sound I have managed to stop it by disposing of the sound object
AudioServicesDisposeSystemSoundID(soundFileObject);
The only thing I still can't do is get my alarm tone to override the mute switch, Audio Sessions don't seem to have any effect on audio triggered with Audio Services. Don't know if anyone still knows of a way of accomplishing this? One more disadvantage is no sound looping, but I have just looped my sound file in an audio editor :)
Upvotes: 0
Reputation: 1019
You can't play scheduled background sounds yourself when the application is in the background.
The only thing you can do is register a custom alert sound with Apple's multitasking APIs and have it play back at a predefined point in time.
Upvotes: 0