21

I want to play a sound even in silent mode in iPhone.

Can it be done by using AVAudioPlayer (Without using AVAudioSession)

(For ios 3.0+)

Thanks in advance.

Nima
  • 969
  • 3
  • 10
  • 14
  • 4
    If the device is on silent mode, don't play sound. The user has explicitly requested silence! – Jonathan Grynspan Apr 25 '12 at 19:15
  • 6
    @JonathanGrynspan - that makes sense when the user _remembers_ that they requested silence, but if you make apps of an AV nature you will be inundated with this kind of review: "AUDIO DOESN'T WORK ★☆☆☆☆" – Rhythmic Fistman Oct 10 '12 at 15:34

5 Answers5

32

Actually, you can do this. It is controlled via the Audio Session and has nothing to do with AVAudioPlayer itself. Why don't you want to use AudioSession? They play nice together...

In your app, you should initialize the Audio Session, and then you can also tell indicate what kind of audio you intend to play. If you're a music player, then it sort of makes sense that the user would want to hear the audio even with the ring/silent switch enabled.

    AudioSessionInitialize (NULL, NULL, NULL, NULL);
    AudioSessionSetActive(true);

    // Allow playback even if Ring/Silent switch is on mute
    UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
    AudioSessionSetProperty (kAudioSessionProperty_AudioCategory, 
                             sizeof(sessionCategory),&sessionCategory);

I have an app that I do this very thing, and use AVAudioPlayer to play audio, and with the ring/silent switch enabled, I can hear the audio.

UPDATE (11/6/2013)

In the app I mentioned above, where I used the code above successfully, I have (for some time) been using the following code instead to achieve the same result:

        AVAudioSession *audioSession = [AVAudioSession sharedInstance];
        NSError *error = nil;
        BOOL result = NO;
        if ([audioSession respondsToSelector:@selector(setActive:withOptions:error:)]) {
            result = [audioSession setActive:YES withOptions:0 error:&error]; // iOS6+
        } else {
            [audioSession setActive:YES withFlags:0 error:&error]; // iOS5 and below
        }
        if (!result && error) {
            // deal with the error
        }

        error = nil;
        result = [audioSession setCategory:AVAudioSessionCategoryPlayback error:&error];

        if (!result && error) {
            // deal with the error
        }

I thought I'd post this as an alternative, in light of the most recent comment to this answer. :-)

Mark Granoff
  • 16,878
  • 2
  • 59
  • 61
  • I used to use this method but now-a-days it does not seem to work anymore. Has anyone else noticed this? – Allen S Nov 06 '13 at 01:27
  • @iphonic Not sure what to tell you. The code I posted on 11/6/2013 is code I have in an app today, that is on the App Store. And it's on my own phone (big surprise). I hear audio with the app when my phone's Ring/Silent switch it set to Silent (muted). Your volume cannot be all the way down; make sure your volume is turned up. Other than presenting the volume and playback control UI to your user, there is no programmatic way for you to turn up the volume, if that's the issue. – Mark Granoff Dec 14 '13 at 20:50
24

MarkGranoff's solution is correct. However, if you prefer to do it in Obj-c instead of C, the following works as well:

    NSError *error = nil;
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&error];
    [[AVAudioSession sharedInstance] setActive:YES error:&error];
Community
  • 1
  • 1
wL_
  • 997
  • 8
  • 13
5

The above answers are correct. Following is the Swift version.

do {
    try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
    //print("AVAudioSession Category Playback OK")
    do {
        try AVAudioSession.sharedInstance().setActive(true)
        //print("AVAudioSession is Active")
    } catch _ as NSError {
        //print(error.localizedDescription)
    }
} catch _ as NSError {
    //print(error.localizedDescription)
}
SHS
  • 1,414
  • 4
  • 26
  • 43
3

Swift 4 simple version:

try? AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [])
Leon
  • 3,614
  • 1
  • 33
  • 46
2

This will simply do the trick (using AVAudioSession)

try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
Johann Burgess
  • 580
  • 6
  • 17