Play Audio when device in silent mode - ios swift

IosSwiftAudio

Ios Problem Overview


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();

Ios Solutions


Solution 1 - Ios

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

Solution 2 - Ios

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

Solution 3 - Ios

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

Solution 4 - Ios

Swift 4

use this line before play video

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

Solution 5 - Ios

> Swift 4.2

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

Solution 6 - Ios

Swift 5.0.1

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

Solution 7 - Ios

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

Solution 8 - Ios

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

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
QuestionAmsheerView Question on Stackoverflow
Solution 1 - IosrushisanganiView Answer on Stackoverflow
Solution 2 - IosmaxwellView Answer on Stackoverflow
Solution 3 - IosSaumil ShahView Answer on Stackoverflow
Solution 4 - Iosfaraz khonsariView Answer on Stackoverflow
Solution 5 - IosYogendra SinghView Answer on Stackoverflow
Solution 6 - IosbytepunchView Answer on Stackoverflow
Solution 7 - IosAsha AntonyView Answer on Stackoverflow
Solution 8 - IosMoin ShiraziView Answer on Stackoverflow