如何更改 AVMutableComposition 中曲目的音量
我一直在尝试不同的方法来解决这个问题,但它们需要很长时间才能处理(更改不同卷的 MP3 文件)。
我有一个 AVMutableComposition,其中填充了多个用于音频和视频的 AVMutableCompositionTrack。混合工作正常,但音轨音量调整不起作用,并且在导出时失败。
这是我使用的代码:
AVMutableComposition* mixComposition = [AVMutableComposition composition];
AVURLAsset *soundTrackAsset = [[AVURLAsset alloc]initWithURL:trackTempProcessedURL options:nil];
//ADDING AUDIO
AVMutableCompositionTrack *compositionAudioSoundTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:trackIDSoundTrack];
[compositionAudioSoundTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAsset.duration)
ofTrack:[[soundTrackAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]
atTime:CMTimeAdd(cmTimeDifference,startTime) error:nil];
NSArray *tracksToDuck = [mixComposition tracksWithMediaType:AVMediaTypeAudio];
NSMutableArray *trackMixArray = [NSMutableArray array];
for (NSInteger i = 0; i < [tracksToDuck count]; i++) {
AVMutableAudioMixInputParameters *trackMix = [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:[tracksToDuck objectAtIndex:i]];
[trackMix setVolume:volume atTime:kCMTimeZero];
[trackMixArray addObject:trackMix];
}
audioMix = [AVMutableAudioMix audioMix];
audioMix.inputParameters = trackMixArray;
//ADDING VIDEO
AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:frontAssetURL options:nil];
[compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration)
ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]
atTime:startTime error:nil];
//EXPORTING
_assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName: AVAssetExportPresetPassthrough];
_assetExport.outputFileType = AVFileTypeQuickTimeMovie;
_assetExport.outputURL = exportUrl;
_assetExport.shouldOptimizeForNetworkUse = YES;
_assetExport.audioMix = audioMix;
[_assetExport exportAsynchronouslyWithCompletionHandler:
^(void ) {
...
没有音频混合器,一切都混合得很好,但是当我尝试改变音量时,导出会出现错误:
AVFoundationErrorDomain Error: 11822
I have been trying different approaches to this problem, but they take too long to process (alter the MP3 file for different volumes).
I have an AVMutableComposition that gets filled with several AVMutableCompositionTrack for audio and video. The mixing works fine but the adjustment in the volume for the audio track is not working and fails when exporting.
Here is the code I use:
AVMutableComposition* mixComposition = [AVMutableComposition composition];
AVURLAsset *soundTrackAsset = [[AVURLAsset alloc]initWithURL:trackTempProcessedURL options:nil];
//ADDING AUDIO
AVMutableCompositionTrack *compositionAudioSoundTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:trackIDSoundTrack];
[compositionAudioSoundTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAsset.duration)
ofTrack:[[soundTrackAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]
atTime:CMTimeAdd(cmTimeDifference,startTime) error:nil];
NSArray *tracksToDuck = [mixComposition tracksWithMediaType:AVMediaTypeAudio];
NSMutableArray *trackMixArray = [NSMutableArray array];
for (NSInteger i = 0; i < [tracksToDuck count]; i++) {
AVMutableAudioMixInputParameters *trackMix = [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:[tracksToDuck objectAtIndex:i]];
[trackMix setVolume:volume atTime:kCMTimeZero];
[trackMixArray addObject:trackMix];
}
audioMix = [AVMutableAudioMix audioMix];
audioMix.inputParameters = trackMixArray;
//ADDING VIDEO
AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:frontAssetURL options:nil];
[compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration)
ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]
atTime:startTime error:nil];
//EXPORTING
_assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName: AVAssetExportPresetPassthrough];
_assetExport.outputFileType = AVFileTypeQuickTimeMovie;
_assetExport.outputURL = exportUrl;
_assetExport.shouldOptimizeForNetworkUse = YES;
_assetExport.audioMix = audioMix;
[_assetExport exportAsynchronouslyWithCompletionHandler:
^(void ) {
...
Everything mixes fine without the audio mixer but when I try to alter the volume the export gives me an error:
AVFoundationErrorDomain Error: 11822
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
AVMutableAudioMixInputParameters 需要设置“trackID”来指示哪个音轨应应用该参数。
AVMutableAudioMixInputParameters need set the "trackID" to indicates which audio track should applied the parameters.