Amsheer
Amsheer

Reputation: 7141

Play Audio when device in silent mode - ios swift

I am creating an application using xcode 7.1, swift. I want to play an audio. Everything is fine. Now my problem I want to hear sound when the device in silent mode or muted. How can I do it?

I am using the following code to play audio

currentAudio!.stop()
currentAudio = try? AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("sample_audio", ofType: "mp3")!));
            
currentAudio!.currentTime = 0
currentAudio!.play();

Upvotes: 64

Views: 43612

Answers (8)

maxwell
maxwell

Reputation: 4166

You can use AppDelegate class.

For enable sound (for audio or video) when device is in silent mode use AVAudioSessionCategoryPlayback:

func applicationDidBecomeActive(_ application: UIApplication) {  
    do {
        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
    } catch {
        print("AVAudioSessionCategoryPlayback not work")
    }
}

For disable sound when device is in silent mode (for example when we answer the phone call) use AVAudioSessionCategorySoloAmbient:

func applicationWillResignActive(_ application: UIApplication) {
    do {
        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategorySoloAmbient)
    } catch {
        print("AVAudioSessionCategorySoloAmbient not work")
    }
}

Upvotes: 28

Asha Antony
Asha Antony

Reputation: 451

import AVKit

And add this to your AppDelegate's applicationDidBecomeActive section

   func applicationDidBecomeActive(_ application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
        do {
            try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback)
            print("AVAudioSession Category Playback OK")
            do {
                try AVAudioSession.sharedInstance().setActive(true)
                print("AVAudioSession is Active")
            } catch {
                print(error.localizedDescription)
            }
        } catch {
            print(error.localizedDescription)
        }
    }

Upvotes: 3

Saumil Shah
Saumil Shah

Reputation: 2349

Swift 3.0 and Xcode > 8

Play sound in Video when device is on RINGER mode and SLIENT mode

    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: 8

rushisangani
rushisangani

Reputation: 3395

Put this line before calling play() method of AVPlayer.

In Objective C

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

In Swift

do {
    try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
} catch {
    // report for an error
}

Swift 5

do {
    try AVAudioSession.sharedInstance().setCategory(.playback)
} catch(let error) {
    print(error.localizedDescription)
}

Upvotes: 179

Moin Shirazi
Moin Shirazi

Reputation: 4425

You can go through this, it will help you out

When you use following audio session categories, sounds will not be muted on iOS: AVAudioSessionCategoryPlayback,AVAudioSessionCategoryRecord,AVAudioSessionCategoryPlayAndRecord

Example

    func playSound (Sound: String, Type: String) {

        //Prepare the sound file name & extension
        var alertSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(Sound, ofType: Type)!)

        //Preparation to play
        AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil)
        AVAudioSession.sharedInstance().setActive(true, error: nil)

        //Play audio

        var error: NSError?
        audioPlayer = AVAudioPlayer(contentsOfURL: alertSound, error: &error)
        audioPlayer.prepareToPlay()
        audioPlayer.play()
    }

Upvotes: 3

yokobuto
yokobuto

Reputation: 81

Swift 5.0.1

try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback)

Upvotes: 4

Yogendra Singh
Yogendra Singh

Reputation: 2241

Swift 4.2

   do {
        try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [])
    } catch let error {
        print("Error in AVAudio Session\(error.localizedDescription)")
    }

Upvotes: 6

faraz khonsari
faraz khonsari

Reputation: 1954

Swift 4

use this line before play video

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

Upvotes: 11

Related Questions