3

我有一个流式传输 mov 文件的应用程序。我的应用程序中的一项功能是在这些流式文件上录制音频。为了实现这一点,我在后台下载流式文件,然后开始录音以捕获用户音频。用户完成后,我将录制的音频与我之前在后台下载的 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 设备将其音频输出设置为蓝牙设备时,也会发生类似的情况。最终录制捕获用户录制的音频和 mov 文件中的视频,但不捕获 mov 文件中的音频。是什么赋予了?

4

1 回答 1

0

addMutableTrackWithMediaType添加指定媒体类型的轨道。通过指定AVMediaTypeVideo我何时添加视频轨道,它只会添加视频轨道。因此,当耳机未插入时,录音机会从正在播放的 mov 中拾取音频,但是,当耳机插入时,麦克风无法拾取通过耳塞播放的音频。

解决方案是添加缓存的mov文件的音频和视频轨道,并处理所有轨道的滞后/同步。如果插入耳机或设备上的音频输出设置为蓝牙扬声器之类的东西,则还应有条件地添加缓存的 mov 的音频,在这种情况下,录音机将无法干净地拾取该音频。

接受其他解决方案/建议。但是这种方法现在对我有用。

于 2016-07-02T15:53:42.700 回答