gpt4 book ai didi

iphone - 在 AVMutableComposition 上设置轨道级别

转载 作者:行者123 更新时间:2023-12-03 18:45:55 28 4
gpt4 key购买 nike

我正在尝试将 4 个音轨组合成一个乐曲,然后将该乐曲导出到一个文件。到目前为止,我的文件已成功创建,但所有音轨都以全音量播放,而不是我尝试设置的音量级别。这是我现在正在做的事情:

AVMutableComposition *trackComposition = [AVMutableComposition composition];

AVAsset *asset1 = ...
AVAsset *asset2 = ...
AVAsset *asset3 = ...
AVAsset *asset4 = ...

NSMutableArray *inputParams = [NSMutableArray arrayWithCapacity:4];

// Add 4 tracks to composition (but only if there are no errors and the track isn't muted
NSError *err;
if(asset1 && ![self trackIsMuted:1]){
AVAssetTrack *rawTrack = [[asset1 tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
AVMutableCompositionTrack *compositionAudioTrack = [trackComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
[compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, [asset1 duration]) ofTrack:rawTrack atTime:kCMTimeZero error:&err];
AVAudioMixInputParameters *audioParams = [self audioParamsForTrack:compositionAudioTrack volume:[self gainForTrack:1]];
[inputParams addObject:audioParams];
}
if(asset2 && !err && ![self trackIsMuted:2]){
AVAssetTrack *rawTrack = [[asset2 tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
AVMutableCompositionTrack *compositionAudioTrack = [trackComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
[compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, [asset2 duration]) ofTrack:rawTrack atTime:kCMTimeZero error:&err];
AVAudioMixInputParameters *audioParams = [self audioParamsForTrack:compositionAudioTrack volume:[self gainForTrack:2]];
[inputParams addObject:audioParams];
}
if(asset3 && !err && ![self trackIsMuted:3]){
AVAssetTrack *rawTrack = [[asset3 tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
AVMutableCompositionTrack *compositionAudioTrack = [trackComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
[compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, [asset3 duration]) ofTrack:rawTrack atTime:kCMTimeZero error:&err];
AVAudioMixInputParameters *audioParams = [self audioParamsForTrack:compositionAudioTrack volume:[self gainForTrack:3]];
[inputParams addObject:audioParams];
}
if(asset4 && !err && ![self trackIsMuted:4]){
AVAssetTrack *rawTrack = [[asset4 tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
AVMutableCompositionTrack *compositionAudioTrack = [trackComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
[compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, [asset4 duration]) ofTrack:rawTrack atTime:kCMTimeZero error:&err];
AVAudioMixInputParameters *audioParams = [self audioParamsForTrack:compositionAudioTrack volume:[self gainForTrack:4]];
[inputParams addObject:audioParams];
}

AVMutableAudioMix *audioMix = [AVMutableAudioMix audioMix];
audioMix.inputParameters = inputParams;

// Export the composition to a file
AVAssetExportSession *export = [AVAssetExportSession exportSessionWithAsset:trackComposition presetName:AVAssetExportPresetAppleM4A];

NSURL *outputURL = [NSURL fileURLWithPath:[[NSTemporaryDirectory() stringByAppendingPathComponent:[NSString guidString]] stringByAppendingPathExtension:@"m4a"]];

[export setOutputURL:outputURL];
[export setOutputFileType:@"com.apple.m4a-audio"];

[export setAudioMix:audioMix];

[export exportAsynchronouslyWithCompletionHandler:^{ ... }];

唯一有趣的是audioParamsForTrack方法,它在这里:

- (AVAudioMixInputParameters *)audioParamsForTrack:(AVAssetTrack *)track volume:(float)vol{
AVMutableAudioMixInputParameters *audioInputParams = [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:track];
[audioInputParams setVolume:vol atTime:kCMTimeZero];

return [audioInputParams copy];
}

有人能发现我做错了什么吗?我尝试传递各种不同的轨道来创建音频参数,但似乎没有什么区别。我看到了有关轨道首选音量的信息 - 这对我有帮助吗?我现在有点卡住了,感谢任何反馈!

最佳答案

我遇到了类似的问题,但对我有用的是在输入参数上显式设置轨道 ID:

    [audioInputParams setTrackID:compositionAudioTrack.trackID];

关于iphone - 在 AVMutableComposition 上设置轨道级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10181403/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com