Nima
Nima

Reputation: 1025

How to Play a sound using AVAudioPlayer when in Silent Mode in iPhone

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.

Upvotes: 21

Views: 16089

Answers (6)

Erhan Demirci
Erhan Demirci

Reputation: 4209

Swift 5 : inside of AppDelegate didFinishLaunchingWithOptions

 do {
                try AVAudioSession.sharedInstance().setCategory(.playback)
                try AVAudioSession.sharedInstance().setActive(true)
                
                // Allow playback even if Ring/Silent switch is on mute
                let sessionCategory = AVAudioSession.Category.playback.rawValue
                let defaultValue: AVAudioSession.Category = .ambient
                try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category(rawValue: sessionCategory) ?? defaultValue)
    
            } catch {
                print("AVAudioSession error: \(error.localizedDescription)")
                
            }

Upvotes: 0

Leon
Leon

Reputation: 3726

Swift 4 simple version:

try? AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [])

Upvotes: 3

Johann Burgess
Johann Burgess

Reputation: 606

This will simply do the trick (using AVAudioSession)

try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)

Upvotes: 2

SHS
SHS

Reputation: 1412

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

Upvotes: 5

Mark Granoff
Mark Granoff

Reputation: 16938

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

Upvotes: 32

wL_
wL_

Reputation: 1007

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

Upvotes: 24

Related Questions