如何使用AVSession录制视频时使音频静音?

时间:2018-10-10 05:11:33

标签: objective-c avplayer

我正在尝试创建类似dubsmash的应用。我要在录制视频和只播放自定义音频时使音频静音。我正在使用AVPlayer播放自定义音频。我的问题是静音,仅录制视频,不录制自定义音频。我尝试设置AVAudioSessionCategoryPlayback,但是它们都使两种声音静音。请提出一些想法

1 个答案:

答案 0 :(得分:0)

正在合并音频和视频:

-(NSURL *)videoAndAudioMergin:(NSURL *)videoTrack
{
    NSURL *playerurl = [[NSBundle mainBundle] URLForResource:trackName
                                               withExtension:@"mp3"
                                                subdirectory:nil];
    AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:playerurl options:nil];
    AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:videoTrack options:nil];

    AVMutableComposition* mixComposition = [AVMutableComposition composition];

    AVMutableCompositionTrack *compositionCommentaryTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio
                                                                                        preferredTrackID:kCMPersistentTrackID_Invalid];
    [compositionCommentaryTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAsset.duration)
                                        ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]
                                         atTime:kCMTimeZero error:nil];

    AVMutableCompositionTrack *compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo
                                                                                   preferredTrackID:kCMPersistentTrackID_Invalid];
    [compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration)
                                   ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]
                                    atTime:kCMTimeZero error:nil];

    AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition
                                                                          presetName:AVAssetExportPresetHighestQuality];

    NSString* videoName = @"export.mov";

    NSString *exportPath = [NSTemporaryDirectory() stringByAppendingPathComponent:videoName];
    NSURL    *exportUrl = [NSURL fileURLWithPath:exportPath];


    return exportUrl;
}