代码之家  ›  专栏  ›  技术社区  ›  user3751111

为什么在使用AVMutableCompositionTrack合并音频和视频时没有声音?[已关闭]

  •  -2
  • user3751111  · 技术社区  · 7 年前

    为什么在使用AVMutableCompositionTrack合并音频和视频时没有声音?

        NSArray * breakArr = [[[[_dic objectForKey:@"url"] lastObject] objectForKey:@"url"]componentsSeparatedByString:@"/"];
        NSString *pathAudio = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:[breakArr lastObject]];
    
        AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:[NSURL fileURLWithPath:pathAudio] options:nil];
    
        NSString *pathVideo = self.pathToMovie;
        AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:[NSURL fileURLWithPath:pathVideo] 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];
    
        if ([[NSFileManager defaultManager] fileExistsAtPath:exportPath])
        {
            [[NSFileManager defaultManager] removeItemAtPath:exportPath error:nil];
        }
    
    //    _assetExport.outputFileType = @"com.apple.quicktime-movie";
        _assetExport.outputFileType = AVFileTypeMPEG4;
        _assetExport.outputURL = exportUrl;
    
        _assetExport.shouldOptimizeForNetworkUse = YES;
    
        [_assetExport exportAsynchronouslyWithCompletionHandler:
             ^(void ) {
                 // your completion code here
                 [[ModelessAlertView instance]closeAlert];
                 UISaveVideoAtPathToSavedPhotosAlbum(exportPath, self, @selector(videoWithPatch:didFinishSavingWithError:contextInfo:), nil);
             }
    
         ];
    

    这是代码。 当我看视频的时候。我只听到音轨的声音。我听不到视频的声音。如何设置音量?

    有人知道吗?非常感谢你。!

    1 回复  |  直到 7 年前
        1
  •  1
  •   Jabson    7 年前

    您的代码工作正常。确保路径上是否存在音频。我尝试从捆绑包中加载声音和视频,并使用您的代码进行合并。正如我所料。我从包裹中装载的货物如下所示。

    NSString *pathAudio = [[NSBundle mainBundle] pathForResource:@"sound" ofType:@"mp3"];
    AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:[NSURL fileURLWithPath:pathAudio] options:nil];
    
    NSString *pathVideo = [[NSBundle mainBundle] pathForResource:@"Clip1" ofType:@"mp4"];
    AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:[NSURL fileURLWithPath:pathVideo] options:nil];
    

    已更新

    为了同时合并视频声音和其他声音,请在AVMutableComposition中添加另一个AVMutableCompositionTrack。

    // add another track for video sound
    AVMutableCompositionTrack *videoSoundTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio
                                                                         preferredTrackID:kCMPersistentTrackID_Invalid];
    
    //insert video sound to the track
    [videoSoundTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration)
                             ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]
                              atTime:kCMTimeZero error:nil];