问题
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 from happening?
Audio session setup:
try! AVAudioSession.sharedInstance().setCategory(.playback, mode: .spokenAudio, options: [.duckOthers, .interruptSpokenAudioAndMixWithOthers])
try! AVAudioSession.sharedInstance().setActive(true)
// ducking starts here, as expected
Video session setup:
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
Full example: https://www.paste.org/98057
来源:https://stackoverflow.com/questions/55601427/avaudiosession-ducking-stops-when-avassetwriter-startwriting-is-called