Play sound on iPhone even in silent mode

Pankaj Kainthla picture Pankaj Kainthla · Sep 18, 2010 · Viewed 57.3k times · Source

I want to make an application which creates sound, music, or system sound when an iPhone is in silent mode. Is it possible to play any type of sound whether music or system tones when it is silent mode?

Answer

Stone Mason picture Stone Mason · Sep 18, 2010

It's not advisable, but who am I to say you can't do it. You may have a good reason to be playing sound.

If you are using Audio Sessions, then include <AVFoundation/AVFoundation.h> at the start of your file and

[[AVAudioSession sharedInstance]
                setCategory: AVAudioSessionCategoryPlayback
                      error: nil];

should do the trick. Note if you play music or sounds, then iPod playback will be paused.

Once this has been done, probably somewhere in the initialization of one of your classes that plays the sounds, you can instantiate sounds like this:

// probably an instance variable: AVAudioPlayer *player;
NSString *path = [[NSBundle mainBundle] pathForResource...];
NSURL *url = [NSURL fileURLWithPath:path];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url];

When that's done, you can play with it any time you want with:

[player play]; // Play the sound
[player pause]; // Pause the sound halfway through playing
player.currentTime += 10 // skip forward 10 seconds
player.duration // Get the duration

And other nice stuff. Look up the AVAudioPlayer Class reference.