2013-04-26 128 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