Airpods导致AVAssetExportSession导出失败

时间:2019-01-23 04:07:54

标签: ios avfoundation airpods

在没有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包含三个错误:

  1. NSLocalizedFailureReason:不支持此媒体格式。
  2. NSLocalizedDescription:无法打开
  3. NSUnderlyingError:错误域= NSOSStatusErrorDomain代码= -16976“(空)”

如何正确处理Airpods?谢谢!

0 个答案:

没有答案