When I call
startWriting
for a video capture session, the ducking for my audio session stops, and the external audio goes back to full volume. How can I prevent this, so that ducking continues?Code example, see lines 9 and 29:
import UIKit
import AVFoundation
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
try! AVAudioSession.sharedInstance().setCategory(.playback, mode: .spokenAudio, options: [.duckOthers, .interruptSpokenAudioAndMixWithOthers])
try! AVAudioSession.sharedInstance().setActive(true)
// ducking starts here, as expected
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
self.beginVideoWritingSession()
}
}
func beginVideoWritingSession(){
let videoSettings:[String: Any] = [AVVideoCodecKey: AVVideoCodecType.h264,
AVVideoWidthKey: 200,
AVVideoHeightKey: 200]
let path = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true)[0] + "/video"
let fileURL = URL(fileURLWithPath: path)
do {
try FileManager.default.removeItem(at: fileURL)
} catch {}
let assetWriter = try! AVAssetWriter(url: fileURL, fileType: AVFileType.mov)
let writeInput = AVAssetWriterInput(mediaType: AVMediaType.video, outputSettings: videoSettings)
assert(assetWriter.canAdd(writeInput))
assetWriter.add(writeInput)
assert(assetWriter.startWriting())
// ducking stops here unexpectedly
}
}