首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在PhotoLibrary iPad中未获得结果影片

在PhotoLibrary iPad中未获得结果影片
EN

Stack Overflow用户
提问于 2012-05-10 15:08:16
回答 1查看 198关注 0票数 0

我正在创建一个具有图像和音频文件数组的电影,并将创建的电影保存到图片库,在模拟器中,我正在使用我的图像和音频文件数组获得正确的电影,但当在设备中运行时,我没有得到我的结果电影,而是我得到了我添加的视频(我在我的代码中使用的OutPut.mov)来写入我的输出文件(混合音频和图像文件数组)。

下面是我将混合音频和图像数组文件保存到图片库的代码:

代码语言:javascript
复制
AVMutableComposition* mixComposition = [AVMutableComposition composition];
NSString* audio_inputFilePath = [[NSBundle mainBundle] pathForResource:@"Ruler" ofType:@"caf"];
NSURL*    audio_inputFileUrl = [NSURL fileURLWithPath:audio_inputFilePath];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;


NSString *video_inputFilePath = [documentsDirectory stringByAppendingPathComponent:@"videoOutput1234videoOutput1234.mp4"];
NSLog(@"output url %@",video_inputFilePath);


NSURL*    video_inputFileUrl = [NSURL fileURLWithPath:video_inputFilePath];


NSString* outputFilePath = [[NSBundle mainBundle] pathForResource:@"OutPut" ofType:@"mov"];
NSURL*    outputFileUrl = [NSURL fileURLWithPath:outputFilePath];

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


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];
CMTimeRange audio_timeRange = CMTimeRangeMake(kCMTimeZero, audioAsset.duration);
AVMutableCompositionTrack *b_compositionAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
[b_compositionAudioTrack insertTimeRange:audio_timeRange ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:nextClipStartTime error:nil];


AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetHighestQuality];   
_assetExport.outputFileType = @"com.apple.quicktime-movie";
_assetExport.outputURL = outputFileUrl;

[_assetExport exportAsynchronouslyWithCompletionHandler:^(void ) {[self saveVideoToAlbum:outputFilePath]; }       ];
EN

回答 1

Stack Overflow用户

发布于 2012-12-31 17:15:13

您必须将视频文件写入到照片库路径中,如下所示。

代码语言:javascript
复制
- (void)writeVideoToPhotoLibrary:(NSURL *)url
{
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

    [library writeVideoAtPathToSavedPhotosAlbum:url completionBlock:^(NSURL *assetURL, NSError *error){
        if (error) {
            NSLog(@"Video could not be saved");
        }
    }];
    [library release];
}

- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    // Let's see what happened
    NSLog(@"finished");
    NSLog(@":error : %@", error);
}

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10529015

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档