tableAudioMix和AVAssetExportSessi

tableAudioMix和AVAssetExportSessi

我想为导出会话设置淡入/淡出效果。但这是行不通的。请帮我!

修剪效果完美!但是淡入/淡出不是:(((

    NSURL *assetURL = [song valueForProperty:MPMediaItemPropertyAssetURL];
        AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL:assetURL options:nil];

    AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]
                                               initWithAsset:songAsset
                                               presetName:AVAssetExportPresetPassthrough];

        NSArray *tracks = [songAsset tracksWithMediaType:AVMediaTypeAudio];
        AVAssetTrack *track = [tracks objectAtIndex:0];
        id desc = [track.formatDescriptions objectAtIndex:0];
        const AudioStreamBasicDescription *audioDesc = CMAudioFormatDescriptionGetStreamBasicDescription((CMAudioFormatDescriptionRef)desc);
        FourCharCode formatID = audioDesc->mFormatID;


    // trim track
        CMTime startTrimTime = CMTimeMakeWithSeconds(startTime, 1);
        CMTime endTrimTime = CMTimeMakeWithSeconds(endTime, 1);
        CMTimeRange exportTimeRange = CMTimeRangeFromTimeToTime(startTrimTime, endTrimTime);
        exportSession.timeRange = exportTimeRange;

    //add fade in, fade out
            CMTime startFadeInTime = startTrimTime;
            CMTime endFadeInTime = CMTimeMakeWithSeconds(startTime + 4.0, 1);
            CMTimeRange fadeInTimeRange = CMTimeRangeFromTimeToTime(startFadeInTime, endFadeInTime);

            CMTime startFadeOutTime = CMTimeMakeWithSeconds(endTime - 4.0, 1);
            CMTime endFadeOutTime = endTrimTime;
            CMTimeRange fadeOutTimeRange = CMTimeRangeFromTimeToTime(startFadeOutTime, endFadeOutTime);

            AVMutableAudioMix *exportAudioMix = [AVMutableAudioMix audioMix];

            //AVMutableAudioMixInputParameters *exportAudioMixInputParameters = [AVMutableAudioMixInputParameters audioMixInputParameters];
            AVMutableAudioMixInputParameters *exportAudioMixInputParameters = [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:track];

            [exportAudioMixInputParameters setVolumeRampFromStartVolume:0.0 toEndVolume:1.0 timeRange:fadeInTimeRange];
            [exportAudioMixInputParameters setVolumeRampFromStartVolume:1.0 toEndVolume:0.0 timeRange:fadeOutTimeRange];

            exportAudioMix.inputParameters = [NSArray arrayWithObject:exportAudioMixInputParameters];
            exportSession.audioMix = exportAudioMix;
...

    myDeleteFile(filePath);
    exportSession.outputURL = [NSURL fileURLWithPath:filePath];


    [exportSession exportAsynchronouslyWithCompletionHandler:^{

        if (exportSession.status == AVAssetExportSessionStatusCompleted) {
            NSLog(@"export session completed");
            //
            //return YES;
        } else {
            NSLog(@"export session error");
            //return NO;
        }

        [exportSession release];
    }];

最佳答案

您可以尝试添加以下行:

[exportAudioMixInputParameters setVolume:0.0 atTime:CMTimeMakeWithSeconds(start-0.01, 1)];


之前:

[exportAudioMixInputParameters setVolumeRampFromStartVolume:0.0 toEndVolume:1.0 timeRange:fadeInTimeRange];


样例代码:

//trim
CMTime startTrimTime = CMTimeMakeWithSeconds(start, 1);
CMTime endTrimTime = CMTimeMakeWithSeconds(start+length, 1);
CMTimeRange exportTimeRange = CMTimeRangeFromTimeToTime(startTrimTime, endTrimTime);
exportSession.timeRange = exportTimeRange;


//add fade in, fade out
AVMutableAudioMix *exportAudioMix = [AVMutableAudioMix audioMix];

NSMutableArray* inputParameters = [NSMutableArray arrayWithCapacity:1];
NSArray *tracks = [asset tracksWithMediaType:AVMediaTypeAudio];
for(AVAssetTrack *track in tracks) {
    //NSLog(@"track:%@", track);
    CMTime startFadeInTime = startTrimTime;
    CMTime endFadeInTime = CMTimeMakeWithSeconds(start + 4.0, 1);
    CMTimeRange fadeInTimeRange = CMTimeRangeFromTimeToTime(startFadeInTime, endFadeInTime);

    CMTime startFadeOutTime = CMTimeMakeWithSeconds(start+length - 4.0, 1);
    CMTime endFadeOutTime = endTrimTime;
    CMTimeRange fadeOutTimeRange = CMTimeRangeFromTimeToTime(startFadeOutTime, endFadeOutTime);

    //AVMutableAudioMixInputParameters *exportAudioMixInputParameters = [AVMutableAudioMixInputParameters audioMixInputParameters];
    AVMutableAudioMixInputParameters *exportAudioMixInputParameters = [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:track];

    [exportAudioMixInputParameters setVolume:0.0 atTime:CMTimeMakeWithSeconds(start-0.01, 1)];
    [exportAudioMixInputParameters setVolumeRampFromStartVolume:0.0 toEndVolume:1.0 timeRange:fadeInTimeRange];
    [exportAudioMixInputParameters setVolumeRampFromStartVolume:1.0 toEndVolume:0.0 timeRange:fadeOutTimeRange];

    [inputParameters addObject:exportAudioMixInputParameters];
}

exportAudioMix.inputParameters = inputParameters;
exportSession.audioMix = exportAudioMix;

关于iphone - AVMutableAudioMix和AVAssetExportSession,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4025950/

10-11 09:11