ios – 使用AVMutableComposition编写视频和音频

ios – 使用AVMutableComposition编写视频和音频,第1张

概述我有一个奇怪的问题.在我的应用程序中,我使用下面的代码组合多个音频视频文件.一旦我将它从设备下载到计算机并使用Quick Time播放,结果视频似乎工作正常,但每当我尝试使用UIWebView或AVPLayer播放新编写的视频时,我只能看到合并视频文件的第一部分. 此外,当我尝试使用MPMoviePlayerController播放时,它会挂起“正在加载”. 我可以听到所有作曲的音频.为了说清楚 我有一个奇怪的问题.在我的应用程序中,我使用下面的代码组合多个音频和视频文件.一旦我将它从设备下载到计算机并使用Quick Time播放,结果视频似乎工作正常,但每当我尝试使用UIWebVIEw或AVPLayer播放新编写的视频时,我只能看到合并视频文件的第一部分.

此外,当我尝试使用MPMovIEPlayerController播放时,它会挂起“正在加载”.

我可以听到所有作曲的音频.为了说清楚,我有两个数组:
1- audioPIEces,包含音频文件的路径[song1,song2,song3];
2- movIEPIEces,包含视频文件的路径[movIE1,movIE2,movIE3];
合并这些文件后,我只能看到movIE1,但我可以听到song1 song2 song3.
附:歌曲和电影有不同的长度(差异小于0.2秒).
任何帮助将不胜感激.
先感谢您,
雅努什

-(voID)putfilesTogether{AVMutableComposition *mixComposition = [AVMutableComposition composition]; AVMutableCompositionTrack *vIDeoCompositionTrack =[[AVMutableCompositionTrack alloc]init];AVMutableCompositionTrack *audioCompositionTrack =[[AVMutableCompositionTrack alloc]init];NSLog(@" movIE  %@ audio %@ ",movIEPIEces,audioPIEces);NSError * error;for(int i=0;i<movIEPIEces.count;i++){    NSfileManager * fm = [NSfileManager defaultManager];    Nsstring * movIEfilePath;    Nsstring * audiofilePath;    movIEfilePath = [movIEPIEces objectAtIndex:i];    audiofilePath = [audioPIEces objectAtIndex:i];    if(![fm fileExistsAtPath:movIEfilePath]){        NSLog(@"MovIE doesn't exist %@ ",movIEfilePath);    }    else{        NSLog(@"MovIE exist %@ ",movIEfilePath);    }    if(![fm fileExistsAtPath:audiofilePath]){        NSLog(@"Audio doesn't exist %@ ",audiofilePath);    }    else{        NSLog(@"Audio exists %@ ",audiofilePath);    }   NSURL *vIDeoUrl = [NSURL fileURLWithPath:movIEfilePath];   NSURL *audioUrl = [NSURL fileURLWithPath:audiofilePath];    AVURLAsset *vIDeoasset = [[AVURLAsset alloc]initWithURL:vIDeoUrl options:nil];     AVAssetTrack *vIDeoAssetTrack= [[vIDeoasset tracksWithMediaType:AVMediaTypeVIDeo] lastObject];    AVURLAsset *audioasset = [[AVURLAsset alloc]initWithURL:audioUrl options:nil];    AVAssetTrack *audioAssetTrack= [[audioasset tracksWithMediaType:AVMediaTypeAudio] lastObject];    vIDeoCompositionTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVIDeo preferredTrackID:kCMPersistentTrackID_InvalID];    audioCompositionTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_InvalID];    CMTime tempTime = mixComposition.duration;    [audioCompositionTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero,audioasset.duration) ofTrack:audioAssetTrack atTime:tempTime error:&error];    [vIDeoCompositionTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero,vIDeoasset.duration) ofTrack:vIDeoAssetTrack atTime:tempTime error:&error];    if(error)    {        NSLog(@"Ups. Something went wrong! %@",[error deBUGDescription]);    }}NSDate *Now = [NSDate dateWithTimeIntervalSinceNow:0];Nsstring *caldate = [Now description];float ran = arc4random()%1000;Nsstring * pathToSave = [Nsstring stringWithFormat:@"Output%@%f.mp4",caldate,ran];pathToSave =[documentS_FolDER stringByAppendingPathComponent:pathToSave];NSURL *movIEUrl = [NSURL fileURLWithPath:pathToSave];AVAssetExportSession *exporter =[[AVAssetExportSession alloc] initWithAsset:mixComposition presetname:AVAssetExportPresetPassthrough];exporter.outputfileType=AVfileTypeQuickTimeMovIE;exporter.outputURL=movIEUrl;exporter.shouldOptimizeforNetworkUse=YES;CMTimeValue val = mixComposition.duration.value;CMTime start=CMTimeMake(0,600);CMTime duration=CMTimeMake(val,600);CMTimeRange range=CMTimeRangeMake(start,duration);exporter.timeRange=range;[exporter exportAsynchronouslyWithCompletionHandler:^{    switch ([exporter status]) {        case AVAssetExportSessionStatusFailed:{            NSLog(@"Export Failed: %@ %@",[[exporter error] localizedDescription],[[exporter error]deBUGDescription]);            Nsstring * message = @"MovIE wasn't created. Try again later.";            [self performSelectorOnMainThread:@selector(dismissMe:) withObject:message waitUntilDone:NO];            break;}        case AVAssetExportSessionStatusCancelled:{ NSLog(@"Export canceled");            Nsstring * message1 = @"MovIE wasn't created. Try again later.";            [self performSelectorOnMainThread:@selector(dismissMe:) withObject:message1 waitUntilDone:NO];            break;}        case AVAssetExportSessionStatusCompleted:        {            Nsstring * message = @"MovIE was successfully created.";            CMTime duration = mixComposition.duration;            [self saveData:duration ofPath:pathToSave];            [self cleanfiles];            [self performSelectorOnMainThread:@selector(dismissMe:) withObject:message waitUntilDone:NO];        }    }}];

}

解决方法 问题在于:
vIDeoCompositionTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVIDeo preferredTrackID:kCMPersistentTrackID_InvalID];audioCompositionTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_InvalID];

它们需要移动到for循环体外.

总结

以上是内存溢出为你收集整理的ios – 使用AVMutableComposition编写视频和音频全部内容,希望文章能够帮你解决ios – 使用AVMutableComposition编写视频和音频所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1108106.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存