简体   繁体   中英

Volume of audio while recording video

So after a lot of searching I was able to find the code block that allows background audio to play while at the same time recording video. I have pasted said code block below.

fileprivate func setBackgroundAudioPreference() {
    guard allowBackgroundAudio == true else {
        return
    }

    guard audioEnabled == true else {
        return
    }

    do{
        if #available(iOS 10.0, *) {
            try AVAudioSession.sharedInstance().setCategory(.playAndRecord, mode: .default, options: [.mixWithOthers, .allowBluetooth, .allowAirPlay, .allowBluetoothA2DP])
        } else {
            let options: [AVAudioSession.CategoryOptions] = [.mixWithOthers, .allowBluetooth]
            let category = AVAudioSession.Category.playAndRecord
            let selector = NSSelectorFromString("setCategory:withOptions:error:")
            AVAudioSession.sharedInstance().perform(selector, with: category, with: options)
        }
        try AVAudioSession.sharedInstance().setActive(true)
        session.automaticallyConfiguresApplicationAudioSession = false
    }
    catch {
        print("[SwiftyCam]: Failed to set background audio preference")

    }
}

However, I have one small issue. For some reason when the camera loads the background Audio volume is reduced. When I record a video with instagram the audio doesn't get reduced and it still records is there any way I can change my current code block to not lower the volume while recoding with video?

I read the documentation and apparently .duckOthers option should be the only option that reduces the volume. But this one does as well

Okay so I found the answer after diving further into some of the documentation.

Updated code posted below. All you have to do is set the .defaultToSpeaker option

fileprivate func setBackgroundAudioPreference() {
    guard allowBackgroundAudio == true else {
        return
    }

    guard audioEnabled == true else {
        return
    }

    do{
        if #available(iOS 10.0, *) {
            try AVAudioSession.sharedInstance().setCategory(.playAndRecord, mode: .default, options: [.mixWithOthers, .allowBluetooth, .allowAirPlay, .allowBluetoothA2DP,.defaultToSpeaker])
        } else {
            let options: [AVAudioSession.CategoryOptions] = [.mixWithOthers, .allowBluetooth]
            let category = AVAudioSession.Category.playAndRecord
            let selector = NSSelectorFromString("setCategory:withOptions:error:")
            AVAudioSession.sharedInstance().perform(selector, with: category, with: options)
        }
        try AVAudioSession.sharedInstance().setActive(true)
        session.automaticallyConfiguresApplicationAudioSession = false
    }
    catch {
        print("[SwiftyCam]: Failed to set background audio preference")

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM