在没有Airpods的情况下进行录制和导出工作正常,但是在使用Airpods时失败。
我正确设置了AVAudioSession模式。
private func defaultRecordSetting() {
do {
try AVAudioSession.sharedInstance().setCategory(.playAndRecord, mode: .default, options: [.mixWithOthers, .duckOthers, .allowBluetooth, .defaultToSpeaker, .interruptSpokenAudioAndMixWithOthers, .allowBluetoothA2DP, .allowAirPlay])
try AVAudioSession.sharedInstance().setActive(true)
} catch {
//Didn't work
}
}
private func mergeClips(clipsCount: Int) {
let assets = clipFiles.map{ AVURLAsset(url: $0) }
mergeAssets(arrayVideos: assets) { session in
switch session.status {
case .completed:
print("Good!")
default:
if let error = session.error {
print(error)
}
}
}
}
func mergeAssets(arrayVideos:[AVAsset], completion: @escaping (_ exporter: AVAssetExportSession) -> ()) {
let mainComposition = AVMutableComposition()
let compositionVideoTrack = mainComposition.addMutableTrack(withMediaType: AVMediaType.video, preferredTrackID: kCMPersistentTrackID_Invalid)
let soundtrackTrack = mainComposition.addMutableTrack(withMediaType: AVMediaType.audio, preferredTrackID: kCMPersistentTrackID_Invalid)
var insertTime = CMTime.zero
for videoAsset in arrayVideos {
for track in videoAsset.tracks(withMediaType: AVMediaType.video) {
try! compositionVideoTrack?.insertTimeRange(CMTimeRangeMake(start: CMTime.zero, duration: videoAsset.duration), of: track, at: insertTime)
}
for track in videoAsset.tracks(withMediaType: AVMediaType.audio) {
try! soundtrackTrack?.insertTimeRange(CMTimeRangeMake(start: CMTime.zero, duration: videoAsset.duration), of: track, at: insertTime)
}
insertTime = CMTimeAdd(insertTime, videoAsset.duration)
}
let exporter = AVAssetExportSession(asset: mainComposition, presetName: AVAssetExportPresetHighestQuality)
exporter?.outputURL = FILE_URL
exporter?.outputFileType = AVFileType.mp4
exporter?.shouldOptimizeForNetworkUse = true
exporter?.exportAsynchronously {
DispatchQueue.main.async {
completion(exporter!)
}
}
}
AVAssetExportSession export异步失败的错误的userInfo包含三个错误:
如何正确处理Airpods?谢谢!