Play sound on iPhone even in silent mode

IphoneObjective CIpadAvaudioplayer

Iphone Problem Overview


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?

Iphone Solutions


Solution 1 - Iphone

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.

Solution 2 - Iphone

And for Swift 2, 3, 4.2

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

Solution 3 - Iphone

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

Solution 4 - Iphone

Works on iOS 6 and above

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

Solution 5 - Iphone

Just put this in your viewDidLoad:

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

Solution 6 - Iphone

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.

Solution 7 - Iphone

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];

Solution 8 - Iphone

Swift - Call this function before playing your audio/video to force playback when the phone is on silent. This also silences other audio that is currently playing.

You can change .duckOthers to .mixWithOthers if you don't want to silence other audio.

func setupAudio() {
  let audioSession = AVAudioSession.sharedInstance()
  _ = try? audioSession.setCategory(AVAudioSessionCategoryPlayback, with: .duckOthers)
  _ = try? audioSession.setActive(true)
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPankaj KainthlaView Question on Stackoverflow
Solution 1 - IphoneStone MasonView Answer on Stackoverflow
Solution 2 - IphoneTomView Answer on Stackoverflow
Solution 3 - IphonePavanView Answer on Stackoverflow
Solution 4 - IphonehfossliView Answer on Stackoverflow
Solution 5 - IphoneDexView Answer on Stackoverflow
Solution 6 - IphoneJeetView Answer on Stackoverflow
Solution 7 - IphoneDennis LanView Answer on Stackoverflow
Solution 8 - IphonebudiDinoView Answer on Stackoverflow