在目标C中将两个视频合并为一个视频后,音频无法播放

时间:2016-05-16 07:42:25

标签: ios objective-c audio video avurlasset

我想将两个或多个视频合并为一个。 我编写了这个并使用下面的代码,我能够这样做。 但是,我的问题是,在将两个或多个视频合并为一个音频后,播放合并视频时无法播放。

所以,请帮助我。

我已将所有视频网址添加到来自AppDelegate文件的数组中。

-(void)MergeVideo
{
    AppDelegate *appdel = (AppDelegate*)[[UIApplication sharedApplication] delegate];

    AVURLAsset *video01;
    AVURLAsset *video02;
    CGFloat totalDuration;
    totalDuration = 0;

    AVMutableComposition *composition = [AVMutableComposition composition];
AVMutableCompositionTrack *composedTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];

    for (int i = 0; i < [appdel.arrVideoPath count]; i++)
    {
        if (i == 0)
        {
            video02 = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:[appdel.arrVideoPath objectAtIndex:i]] options:nil];

            [composedTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, video02.duration) ofTrack:[[video02 tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:kCMTimeZero error:nil];
        }
        else
        {
            video01 = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:[appdel.arrVideoPath objectAtIndex:i-1]] options:nil];
            video02 = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:[appdel.arrVideoPath objectAtIndex:i]] options:nil];

            float duration1 = CMTimeGetSeconds([video01 duration]);
            totalDuration = totalDuration + duration1;
        CMTime time1 = CMTimeMakeWithSeconds(totalDuration, 1);

            [composedTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, video02.duration) ofTrack:[[video02 tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:time1 error:nil];
        }

        NSString* documentsDirectory= [self applicationDocumentsDirectory];
        myDocumentPath= [documentsDirectory stringByAppendingPathComponent:@"merge_video.mp4"];
        urlVideoMain = [[NSURL alloc] initFileURLWithPath: myDocumentPath];

        if([[NSFileManager defaultManager] fileExistsAtPath:myDocumentPath])
        {
            [[NSFileManager defaultManager] removeItemAtPath:myDocumentPath error:nil];
        }

        AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:composition presetName:AVAssetExportPresetHighestQuality];
        exporter.outputURL = urlVideoMain;
        exporter.outputFileType = @"com.apple.quicktime-movie";
        exporter.shouldOptimizeForNetworkUse = YES;

        [exporter exportAsynchronouslyWithCompletionHandler:^{

            switch ([exporter status])
            {
                case AVAssetExportSessionStatusFailed:
                    break;

                case AVAssetExportSessionStatusCancelled:
                    break;

                case AVAssetExportSessionStatusCompleted:
                    break;

                default:
                    break;
            }
        }];
    }
}

- (NSString*) applicationDocumentsDirectory
{
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    return basePath;
}

0 个答案:

没有答案