3

mov ファイルをストリーミングするアプリがあります。私のアプリの機能の 1 つは、これらのストリーミング ファイルにオーディオを録音することです。これを実現するために、ストリーミング ファイルをバックグラウンドでダウンロードし、オーディオ録音を開始してユーザー オーディオをキャプチャします。ユーザーが完了したら、録音したオーディオを取得し、以前にバックグラウンドでダウンロードした mov ファイルとマージします。

ヘッドフォンを接続する場合を除いて、これらはすべて正常に機能しています。エクスペリエンスは同じですが、録音を再生すると、オーディオのみがキャプチャされます。mov ファイルが最終的なアセットになることはありませんが、その理由は不明です。

これが私が録音を作成する方法です:

let video = AVAsset(URL: currentCacheUrl)
let audioRecording = AVAsset(URL: currentRecordingUrl)

// 1 - Create AVMutableComposition object. This object will hold your AVMutableCompositionTrack instances.
let mixComposition = AVMutableComposition()

// 2 - Video track
let videoTrack = mixComposition.addMutableTrackWithMediaType(AVMediaTypeVideo, preferredTrackID: kCMPersistentTrackID_Invalid)
do {
    try videoTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero, audioRecording.duration),
                                   ofTrack: video.tracksWithMediaType(AVMediaTypeVideo)[0],
                                   atTime: kCMTimeZero)
} catch _ {
    print("Failed to load video track")
}

// 3 - Audio track
let audioTrack = mixComposition.addMutableTrackWithMediaType(AVMediaTypeAudio, preferredTrackID: 0)
do {
    try audioTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero, audioRecording.duration),
                                   ofTrack: audioRecording.tracksWithMediaType(AVMediaTypeAudio)[0],
                                   atTime: kCMTimeZero)
} catch _ {
    print("Failed to load audio track")
}

// 4 - Get path
let recordingsPath = MisueroKaraokeLatinoHelper.Variables.getRecordingsDirectory
let currentDate = NSDate()
let date = formatDate(currentDate)

let savePath = recordingsPath.URLByAppendingPathComponent("\(date).mov")

// 5 - Create Exporter
guard let exporter = AVAssetExportSession(asset: mixComposition, presetName: AVAssetExportPresetHighestQuality) else { return }
exporter.outputURL = savePath
exporter.outputFileType = AVFileTypeQuickTimeMovie
exporter.shouldOptimizeForNetworkUse = true

// 6 - Perform the Export
exporter.exportAsynchronouslyWithCompletionHandler() {
    dispatch_async(dispatch_get_main_queue()) { _ in
        print("save and merge complete")

        let recording = Recording(title: self.currentRecordSong.title, artist: self.currentRecordSong.artist, genre: self.currentRecordSong.genre, timestamp: currentDate, fileUrl: savePath)
        MisueroKaraokeLatinoHelper.Variables.Recordings.append(recording)
        MisueroKaraokeLatinoHelper.Variables.Recordings.sortInPlace({$0.timestamp.compare($1.timestamp) == NSComparisonResult.OrderedDescending})

        let recordingsData = NSKeyedArchiver.archivedDataWithRootObject(MisueroKaraokeLatinoHelper.Variables.Recordings)
        NSUserDefaults.standardUserDefaults().setObject(recordingsData, forKey: "Recordings")
    }
}

iOS デバイスのオーディオ出力が Bluetooth デバイスに設定されている場合にも、同様のことが起こります。最終的な記録では、ユーザーが記録したオーディオとビデオが mov ファイルに取り込まれますが、mov ファイルのオーディオは取り込まれません。何を与える?

4

1 に答える 1