AVAssetExportSession和淡入淡出效果

 BIGBANG-YG-BEAR 发布于 2023-02-08 14:56

我正在使用音频文件 AVAssetExportSession但无法在保存的文件中淡出淡出效果.这是我正在使用的代码.

       [[NSFileManager defaultManager] removeItemAtURL:audioFileOutput error:NULL];
        AVAsset *asset = [AVAsset assetWithURL:[NSURL fileURLWithPath:file_path]];
        AVAssetExportSession *exportSession = [AVAssetExportSession exportSessionWithAsset:asset
                                                                                presetName:AVAssetExportPresetAppleM4A];
        time = floorf(Endtime-startTime);
        CMTime start = CMTimeMake(0.0*100,100);
        CMTime stop = CMTimeMake(time*100,100);
        CMTimeRange exportTimeRange = CMTimeRangeFromTimeToTime(start, stop);
        AVMutableAudioMix *mutableAudioMix = [AVMutableAudioMix audioMix];
        AVURLAsset* audio = [[AVURLAsset alloc]initWithURL:[NSURL URLWithString:file_path] options:nil];
        AVAssetTrack* audioTrack = [[audio tracksWithMediaType:AVMediaTypeAudio]
                                    objectAtIndex:0];
        AVMutableAudioMixInputParameters *mixParameters = [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:audioTrack];
        [mixParameters setVolumeRampFromStartVolume:1.0 toEndVolume:0.0 timeRange:exportTimeRange];
        mutableAudioMix.inputParameters = @[mixParameters];
        exportSession.audioMix = mutableAudioMix;
        exportSession.outputURL = audioFileOutput;
        exportSession.outputFileType = AVFileTypeAppleM4A;
        exportSession.timeRange = exportTimeRange;
        [SVProgressHUD showWithStatus:@"Saving Recording.." maskType:SVProgressHUDMaskTypeBlack];
        [exportSession exportAsynchronouslyWithCompletionHandler:^
         {
             dispatch_async(dispatch_get_main_queue(), ^{
                 if (AVAssetExportSessionStatusCompleted == exportSession.status)
                 {
                     [SVProgressHUD dismiss];
                     NSLog(@"Success!");
                     NSLog(@"saving complete %@", exportSession.description);

                     NSLog(@"the Song Path : %@", strOutputFilePath);

                }
                 else if (AVAssetExportSessionStatusFailed == exportSession.status)
                 {
                     NSLog(@"failed");
                     [SVProgressHUD showErrorWithStatus:@"Record Again"];
                 }
                 [[NSFileManager defaultManager] removeItemAtPath:file_path error:nil];
             });

         }];

我不知道我错过了什么.任何帮助表示赞赏.

1 个回答
  • 那试试这样的事

    AVMutableAudioMix *exportAudioMix = [AVMutableAudioMix audioMix];
    AVAssetTrack *assetTrack = [[asset tracksWithMediaType:AVMediaTypeAudio]objectAtIndex:0];
    AVMutableAudioMixInputParameters *exportAudioMixInputParameters = [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:assetTrack];
    exportAudioMixInputParameters.trackID = [[[asset tracksWithMediaType:AVMediaTypeAudio]objectAtIndex:0] trackID];
    NSMutableArray* inputParameters = [NSMutableArray arrayWithCapacity:1];
    
    CMTime startFadeInTime = start;
    CMTime endFadeInTime = CMTimeMake((startTime+2)*100, 100);
    CMTime startFadeOutTime = CMTimeMake((time-2)*100, 100);
    CMTime endFadeOutTime = CMTimeMake(time*100, 100);
    
    CMTimeRange fadeInTimeRange = CMTimeRangeFromTimeToTime(startFadeInTime, endFadeInTime);
    CMTimeRange fadeOutTimeRange = CMTimeRangeFromTimeToTime(startFadeOutTime, endFadeOutTime);
    [exportAudioMixInputParameters setVolumeRampFromStartVolume:0.0 toEndVolume:1.0 timeRange:fadeInTimeRange];
    [exportAudioMixInputParameters setVolumeRampFromStartVolume:1.0 toEndVolume:0.0 timeRange:fadeOutTimeRange];
    
    [inputParameters insertObject:exportAudioMixInputParameters atIndex:0];
    

    希望它会有所帮助.快乐的编码.

    2023-02-08 15:01 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有