Play sound on iPhone even in silent mode

7 Solutions Collect From Internet About “Play sound on iPhone even in silent mode”

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.

Yes you can play sound when the phone is set to vibrate.
Simply use the AVAudioPlayer class.

By default, playing an Audio Session
sound will ~not~ respect the setting
of the mute switch on the iPhone. In
other words, if you make a call to
play a sound and the silent (hardware)
switch on the iPhone is set to silent,
you’ll still hear the sound.

This is what you want. So now you know that playing an Audio Session when your phone is in silent mode will still play the sound you just need to know how to create an audio session to play the sound, like so:
taken from this website: http://iosdevelopertips.com/audio/playing-short-sounds-audio-session-services.html

SystemSoundID soundID;
NSString *path = [[NSBundle mainBundle]
   pathForResource:@"RapidFire" ofType:@"wav"];    

AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path],&soundID);
AudioServicesPlaySystemSound (soundID);

For this to work, you will need to import header file, and also add the AudioToolbox.framework to your project.

And that’s it.

So from this answer you now know that you can play sound while the phone is on vibrate.
You don’t need extra or special code to allow you to do this functionality as it already does that by default.

Pk

Works on iOS 6 and above

NSError *setCategoryErr = nil;
NSError *activationErr  = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&setCategoryErr];
[[AVAudioSession sharedInstance] setActive:YES error:&activationErr];

And for Swift 2 and Swift 3

    do {
      try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
      try AVAudioSession.sharedInstance().setActive(true)
    } catch {
      print(error)
    }

Just put this in your viewDidLoad:

UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty (kAudioSessionProperty_AudioCategory,
                         sizeof(sessionCategory), &sessionCategory);

For playing a sound in silent mode and even when it goes to background, try this:

Put this code in application:didFinishLaunchingWithOpitons:

[[AVAudioSession sharedInstance] setDelegate:self];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

and this code in applicationDidEnterBackground:

[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

Also setup a background mode of Playing Audio/Air Play and include AVFoundation header.

for Objective C, you could play system sound with following code:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
*The system path could be modified.*
NSString *path = @"/System/Library/Audio/UISounds/begin_record.caf";
NSURL *url = [NSURL fileURLWithPath:path];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[player play];