2017-01-22 83 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,加上來自蘋果的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