Search code examples
objective-cavplayer

How to mute audio in recording video using AVSession?


I am trying to create app like dubsmash. I want mute audio in recording video and playing only custom audio. I am playing custom audio using AVPlayer. my problem is mute only recording video only not custom audio. I tried to set AVAudioSessionCategoryPlayback but, they mute both sound. pls suggest some ideas


Solution

  • Merging audio and video is working:

    -(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;
    }