【问题标题】:Audio Mixing in iOS using AVFoundation doesnt work使用 AVFoundation 在 iOS 中进行音频混合不起作用
【发布时间】:2013-08-09 04:05:29
【问题描述】:

我正在尝试将一堆视频拼接在一起,然后在 iOS 中的视频上添加一些音乐。使用AVMutableAudioMix 添加音频。但是,当最终导出视频时,音频混合丢失了。下面是代码的样子:

- (void)mergeVideos:(NSArray *)videos{
    AVMutableComposition *mixComposition = [[AVMutableComposition alloc] init];
    AVMutableCompositionTrack *videoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo
                                                                        preferredTrackID:kCMPersistentTrackID_Invalid];
    AVMutableCompositionTrack *audioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio
                                                                        preferredTrackID:kCMPersistentTrackID_Invalid];
    CMTime currentTime = kCMTimeZero;
    for (AVAsset *asset in videos) {
        // 2 - Video track
        [videoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset.duration)
                            ofTrack:[[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:currentTime error:nil];
        [audioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset.duration)
                            ofTrack:[[asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:currentTime error:nil];
        currentTime = CMTimeAdd(currentTime, asset.duration);
    }
        // 4 - Get path
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *myPathDocs =  [documentsDirectory stringByAppendingPathComponent:
                             [NSString stringWithFormat:@"mergeVideo-%d.mp4",arc4random() % 1000]];
    NSURL *url = [NSURL fileURLWithPath:myPathDocs]; 
    // 5 - Create exporter
    NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:mixComposition];
    NSString *quality = AVAssetExportPresetHighestQuality;
    //load the audio
    AVMutableAudioMix *audioMix = nil;
    NSURL *audioURL = [self loadAudioFile];// gives a url for a .caf file from the bundle
    if (audioURL) {
        AVURLAsset *audioAsset = [AVURLAsset assetWithURL:audioURL];
        AVAssetTrack *aTrack =  (AVAssetTrack *)[[audioAsset tracksWithMediaType:AVMediaTypeAudio] firstObject];

        AVMutableAudioMixInputParameters *trackMix =
        [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:aTrack];

        [trackMix setVolumeRampFromStartVolume:0 toEndVolume:1 timeRange:
         CMTimeRangeMake(CMTimeMakeWithSeconds(0, 1), CMTimeMakeWithSeconds(3, 1))];
        [trackMix setVolume:1.0 atTime:kCMTimeZero];
        AVMutableAudioMix *audioMix = [AVMutableAudioMix audioMix];
        audioMix.inputParameters = [NSArray arrayWithObject:trackMix];
    }

    AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition
                                                                      presetName:quality];
    if (audioMix) {
        exporter.audioMix = audioMix;
    }
    exporter.outputURL=url;
    exporter.outputFileType = AVFileTypeMPEG4;
    exporter.shouldOptimizeForNetworkUse = YES;
    [exporter exportAsynchronouslyWithCompletionHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            [self exportDidFinish:exporter];
        });
    }];
}

执行时没有错误。只是.caf 文件中包含的音乐不会混合到导出的文件中。知道发生了什么吗?

【问题讨论】:

    标签: ios audio avfoundation core-audio video-editing


    【解决方案1】:

    您尚未将 .caf 文件中的音频插入任何 AVMutableCompositionTrack,因此与 aTrack 关联的音频混合不会调整 .caf 文件的音量。如果您希望 .caf 文件中的音频与关联的音频混合一起包含在视频中,请创建另一个 AVMutableCompositionTrack 来保存 .caf 文件中的音频:

    AVMutableCompositionTrack *audioTrackCAF = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio
                                                                        preferredTrackID:kCMPersistentTrackID_Invalid];
    

    (根据您的喜好设置时间范围):

    [audioTrackCAF insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAsset.duration)
                                ofTrack:aTrack atTime:kCMTimeZero error:nil]
    

    此外,将NSError *(而不是nil)传递给insertTimeRange:ofTrack:atTime:error 有助于确保您有有效的时间范围并插入了您的媒体。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-13
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 2015-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多