2017-01-22 2 views
0

저는 빌드 한 AVMutableComposition에서 오디오 편집을 시도하고 있습니다.AVAudioMix 오디오 더킹이 작동하지 않습니다.

 var commentaryTimeRange = CMTimeRange(start: commentaryItem.startTimeInTimeline, duration: commentaryItem.timeRange.duration) 
     if CMTimeCompare(CMTimeRangeGetEnd(commentaryTimeRange), composition.duration) == 1 { 
      commentaryTimeRange.duration = CMTimeSubtract(composition.duration, commentaryTimeRange.start); 
      commentaryItem.timeRange = commentaryTimeRange 
     } 
     // Add the commentary track 
     let compositionCommentaryTrack = composition.addMutableTrack(withMediaType: AVMediaTypeAudio, preferredTrackID: kCMPersistentTrackID_Invalid) 
     let track = commentaryItem.asset.tracks(withMediaType: AVMediaTypeAudio).first! 
     try! compositionCommentaryTrack.insertTimeRange(CMTimeRange(start: kCMTimeZero, duration:commentaryTimeRange.duration), of: track, at: commentaryTimeRange.start) 

     let tracksToDuck = composition.tracks(withMediaType: AVMediaTypeAudio) 
     var trackMixArray = [AVMutableAudioMixInputParameters]() 
     let rampDuration = CMTime(seconds: 1, preferredTimescale: 2) 
     for track in tracksToDuck { 
      let trackMix = AVMutableAudioMixInputParameters(track: track) 
      trackMix.setVolumeRamp(fromStartVolume: 1.0, toEndVolume: 0.2, timeRange: CMTimeRange(start: CMTimeSubtract(commentaryTimeRange.start, rampDuration), duration: CMTimeSubtract(commentaryTimeRange.duration, rampDuration))) 
      trackMix.setVolumeRamp(fromStartVolume: 0.2, toEndVolume: 1.0, timeRange: CMTimeRange(start: CMTimeRangeGetEnd(commentaryTimeRange), duration: rampDuration)) 
      trackMixArray.append(trackMix) 
     } 
     let audioMix = AVMutableAudioMix() 
     audioMix.inputParameters = trackMixArray 


는 기본적으로 나는 원래 볼륨을 더킹하여 비디오 트랙에 주석을 추가 할 트루 잉하고있다.
오디오가 출력에서 ​​올바르게 혼합되었지만 오디오 지시가 무시 된 것 같습니다.
물론 audiomix는 AVPlayerItem으로 전달됩니다. 디버깅에서 모든 명령어가 거기에 있고 올바르게 전달 된 것을 볼 수 있습니다.

func makePlayable() -> AVPlayerItem { 
     let playerItem = AVPlayerItem(asset: composition.copy() as! AVAsset, automaticallyLoadedAssetKeys: NewsPlayerViewController.assetKeysRequiredToPlay) 
     playerItem.videoComposition = videoComposition 
     playerItem.audioMix = audioMix?.copy() as! AVAudioMix? 
     if let overlayLayer = overlayLayer { 
      let syncLayer = AVSynchronizedLayer(playerItem: playerItem) 
      syncLayer.addSublayer(overlayLayer) 
      playerItem.syncLayer = syncLayer 
     } 
     return playerItem 
} 

나는 이유로 트랙 식별자의 부족, 또는 하나를 가지고 구성 사이의 불일치의 종류 및되지 않은 트랙을 나타내는 몇 가지 답을 찾았습니다.
제 구성물에 트랙 ID가 사용되지 않고 Apple의 AVEdit 샘플 코드가이를 사용하지 않고 작동합니다.

답변

0

해결 방법은 단순히 해설 트랙을 추가하기 전에 오리에 트랙을 세는 것이 었습니다.

 let tracksToDuck = composition.tracks(withMediaType: AVMediaTypeAudio)// <- MOVE HERE, AT THE TOP 

     var commentaryTimeRange = CMTimeRange(start: commentaryItem.startTimeInTimeline, duration: commentaryItem.timeRange.duration) 
     if CMTimeCompare(CMTimeRangeGetEnd(commentaryTimeRange), composition.duration) == 1 { 
      commentaryTimeRange.duration = CMTimeSubtract(composition.duration, commentaryTimeRange.start); 
      commentaryItem.timeRange = commentaryTimeRange 
     } 
     // Add the commentary track 
     let compositionCommentaryTrack = composition.addMutableTrack(withMediaType: AVMediaTypeAudio, preferredTrackID: kCMPersistentTrackID_Invalid) 
     let track = commentaryItem.asset.tracks(withMediaType: AVMediaTypeAudio).first! 
     try! compositionCommentaryTrack.insertTimeRange(CMTimeRange(start: kCMTimeZero, duration:commentaryTimeRange.duration), of: track, at: commentaryTimeRange.start) 

     var trackMixArray = [AVMutableAudioMixInputParameters]() 
     let rampDuration = CMTime(seconds: 1, preferredTimescale: 2) 
     for track in tracksToDuck { 
      let trackMix = AVMutableAudioMixInputParameters(track: track) 
      trackMix.setVolumeRamp(fromStartVolume: 1.0, toEndVolume: 0.2, timeRange: CMTimeRange(start: CMTimeSubtract(commentaryTimeRange.start, rampDuration), duration: CMTimeSubtract(commentaryTimeRange.duration, rampDuration))) 
      trackMix.setVolumeRamp(fromStartVolume: 0.2, toEndVolume: 1.0, timeRange: CMTimeRange(start: CMTimeRangeGetEnd(commentaryTimeRange), duration: rampDuration)) 
      trackMixArray.append(trackMix) 
     } 
     let audioMix = AVMutableAudioMix() 
     audioMix.inputParameters = trackMixArray 
+0

답변에 대한 자세한 내용을 입력하십시오. 코드에서 무엇을 변경 했습니까? 나는 비슷한 문제에 직면하고있다. –

+0

@NamitGupta가 올바른 스 니펫을 추가했습니다. – Andrea