简体   繁体   中英

Need an iOS 4 Audio API that can be used like the alarm app

I am working on an alarm application, I need to find the best method of playing a sound that will:

  • Play a looped sound, or pre-looped sound when the app is in the background
  • Can be paused/stopped. When the user has clicked to dismiss the alarm in an alert I need to stop the sound
  • Override the mute switch (preferably)
  • Will respect the volume control (preferably)
  • Will pause the iPod (preferably)

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

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.

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM