Play Audio when device in silent mode - ios swift

前端 未结 8 1894
无人及你
无人及你 2020-12-23 16:58

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

8条回答
  •  醉酒成梦
    2020-12-23 17:28

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

提交回复
热议问题