2013-04-26 132 views
7

我正在使用AVMutableComposition混合音频和视频文件。下面是我的代码为:如果视频持续时间长于音频持续时间,如何重复播放音轨

enter code here 
AVMutableComposition* mixComposition = [AVMutableComposition composition]; 

NSString *bundleDirectory = [[NSBundle mainBundle] bundlePath]; 

NSString *audio_inputFilePath = [bundleDirectory stringByAppendingPathComponent:@"xyz.mp3"];//audio of 35 seconds 

NSURL *audio_inputFileUrl = [NSURL fileURLWithPath:audio_inputFilePath]; 

    NSURL *video_inputFileUrl = [NSURL fileURLWithPath:videoOutputPath]; 

    NSString *outputFilePath = [documentsDirectory stringByAppendingPathComponent:@"video.mp4"];//video of 60 seconds 

NSURL *outputFileUrl = [NSURL fileURLWithPath:outputFilePath]; 

if ([[NSFileManager defaultManager] fileExistsAtPath:outputFilePath]) 
     [[NSFileManager defaultManager] removeItemAtPath:outputFilePath error:nil]; 

    CMTime nextClipStartTime = kCMTimeZero; 

    AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:video_inputFileUrl options:nil]; 

CMTimeRange video_timeRange = CMTimeRangeMake(kCMTimeZero,videoAsset.duration); 

    AVMutableCompositionTrack *a_compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid]; 

[a_compositionVideoTrack insertTimeRange:video_timeRange ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:nextClipStartTime error:nil]; 

    AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:audio_inputFileUrl options:nil]; 
    enter code here 
    CMTimeRange audio_timeRange = CMTimeRangeMake(kCMTimeZero, audioAsset.duration); 
    enter code here 
    AVMutableCompositionTrack *b_compositionAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid]; 
    [b_compositionAudioTrack insertTimeRange:audio_timeRange ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:nextClipStartTime error:nil]; 
    enter code here 
    AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPreset640x480]; 

问题我面临的是我的音频文件持续时间短于视频持续时间。所以我想做的是循环音频文件,直到视频结束。就像我的视频是60秒,音频是35秒,所以音频应该重复25秒。

任何人都可以帮助我如何做到这一点。

回答

4

解决方案1 ​​:在AVMutableCompositionTrack

if (videoAsset.duration>audioAsset.duration) 
{ 
    //new time range 
    CMTime duration = CMTimeMakeWithSeconds(CMTimeGetSeconds(videoAsset.duration)-CMTimeGetSeconds(audioAsset.duration), audioAsset.duration.timescale); 
    if (CMTIME_IS_VALID(duration)) 
    { 
    CMTimeRange video_timeRange2 = CMTimeRangeMake(audioAsset.duration,duration); 
    //start from where left 
    CMTime nextClipStartTime2 = audioAsset.duration; 
    //add in AVMutableCompositionTrack 
    [b_compositionVideoTrack insertTimeRange:video_timeRange2 ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:nextClipStartTime2 error:nil]; 
    } 
    else 
     NSLog(@"time is invalid"); 
} 

注意创建一个新的CMTimeRangeinsertTimeRange:未经测试,但它应该工作。

编辑

解决方案2:试试这个。不要使用我的代码,并用你的代码替换下面的这一行

[b_compositionAudioTrack insertTimeRange:audio_timeRange ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:kCMTimeInvalid error:nil]; 
+0

无法正常工作。你的逻辑似乎是正确的,但仍然音频停止35秒后 – coder1010 2013-04-26 06:35:34

+0

我已经尝试与b_composition。我用你的逻辑。不一样的路线。是否有可能在混合组合中添加两个音轨? – coder1010 2013-04-26 07:00:07

+0

再次阅读我的答案,然后再试一次 – 2013-04-26 07:03:58

相关问题