Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/98.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 将音频与AVAssetExportSession合并_Ios_Swift_Avassetexportsession - Fatal编程技术网

Ios 将音频与AVAssetExportSession合并

Ios 将音频与AVAssetExportSession合并,ios,swift,avassetexportsession,Ios,Swift,Avassetexportsession,我想合并两个音频文件而不需要太多的开销时间。下面的代码成功地合并了音频,但时间太长(超过30秒的音频超过几分钟),我想知道是否有任何方法可以加快这个过程。我在几个地方读到了使用AVAssetExportPresetPassthrough的文章,但我似乎无法将该预设用于任何文件类型。我唯一能够使用的设置是使用AVAssetExportPresetAppleM4A并导出为.m4a 用于创建AVAssetExportSession的代码 if (audioHasBeenRecorded) { //

我想合并两个音频文件而不需要太多的开销时间。下面的代码成功地合并了音频,但时间太长(超过30秒的音频超过几分钟),我想知道是否有任何方法可以加快这个过程。我在几个地方读到了使用AVAssetExportPresetPassthrough的文章,但我似乎无法将该预设用于任何文件类型。我唯一能够使用的设置是使用
AVAssetExportPresetAppleM4A
并导出为
.m4a

用于创建AVAssetExportSession的代码

if (audioHasBeenRecorded) {
  // Merge recordings
  let composition = AVMutableComposition()
  let compositionAudioTrack = composition.addMutableTrack(withMediaType: AVMediaType.audio, preferredTrackID: kCMPersistentTrackID_Invalid)

  guard let start = recorder.timeStart else { return }
  
  compositionAudioTrack?.insert(originalRecording: FileManagerHelper.recordingLocalURL(secondRecording: false), insertedRecording: FileManagerHelper.recordingLocalURL(secondRecording: true), startTime: CMTime(seconds: start, preferredTimescale: 1000000))

  if let assetExport = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetAppleM4A) {
    print(assetExport.supportedFileTypes)
    assetExport.outputFileType = AVFileType.m4a
    assetExport.outputURL = FileManagerHelper.recordingLocalURL(secondRecording: false)
    do { // Delete old audio
        try FileManager.default.removeItem(at: FileManagerHelper.recordingLocalURL(secondRecording: false))
        try FileManager.default.removeItem(at: FileManagerHelper.recordingLocalURL(secondRecording: true))
    } catch { log(error.localizedDescription, msgType: .error) }
   
    assetExport.exportAsynchronously(completionHandler: {
      if let error = assetExport.error {
        log(error, msgType: .error)
      } else {
        log("Successfully merged recordings!", msgType: .error)
        self.idea.numberOfPoints = self.audioVisualizer.count
        self.idea.save()
        
        self.setupPlayer() // Prepare to play the recorded audio file
        self.seekTo(TimeInterval((recorder.timeStart ?? 0) + (recorder.timeEnd ?? 0)))
        DispatchQueue.main.async { [ weak self ] in
          guard let self = self else { return }
          self.audioVisualizer.visualize(self.idea)
        }
      }
    })
  }
}
插入代码:

extension AVMutableCompositionTrack {
  func insert(originalRecording: URL, insertedRecording: URL, startTime: CMTime) {
    let originalAsset = AVURLAsset(url: originalRecording)
    let insertedAsset = AVURLAsset(url: insertedRecording)

    let range1 = CMTimeRangeMake(start: CMTime.zero, duration: startTime)
    let range2 = CMTimeRangeMake(start: CMTime.zero, duration: insertedAsset.duration)
    let range3 = CMTimeRangeMake(start: startTime + insertedAsset.duration, duration: originalAsset.duration - startTime)
    if let originalTrack = originalAsset.tracks(withMediaType: AVMediaType.audio).first,
       let insertedTrack = insertedAsset.tracks(withMediaType: AVMediaType.audio).first {
         try? insertTimeRange(range1, of: originalTrack, at: CMTime.zero)
         try? insertTimeRange(range2, of: insertedTrack, at: startTime)
         try? insertTimeRange(range3, of: originalTrack, at: startTime + insertedAsset.duration)
    }
  }
}

我认为你找错地方了。要合并两个音频文件,请使用AVAudioEngine。嗨,matt,谢谢你的建议。请问从哪里开始寻找AVAudioEngine?似乎我发现AVAudioEngine与播放有关,我在合并音频方面找不到任何东西。在预定时间开始播放多个音频文件的想法是否会产生合并音频的效果?AVAudioEngine是一个编程的声音混音板,因此它可以合并多个声音正是它的用途。您可以将其输出脱机生成为新文件(而不是实时播放输出),这就是您想要做的。是的,如果不同的文件在不同的时间开始播放,你可以安排播放时间。你可以看到我在这里玩AVAudioEngine:对于你的用例,请特别关注
doButton5
。哇,在研究AVAudioEngine时发现了这个资源。我不知道这是你。再次感谢!非常感谢您的帮助:)