将视频保存到iPad视频应用 [英] Save Video to iPad Videos app

查看:225
本文介绍了将视频保存到iPad视频应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用 UISaveVideoAtPathToSavedPhotosAlbum 来保存我的视频(本地存储在应用程序中)。但是当我尝试保存它时,我收到一条错误消息操作失败,因为视频文件无效且无法播放。该文件只有大约一分钟的长度,是一个.mp4文件。使用MPMoviePlayer播放它没有问题,它只是不会保存。以下是代码:

I have been trying to use the UISaveVideoAtPathToSavedPhotosAlbum to save my video (stored locally within the application). But when I try to save it, i get an error saying "Operation failed because video file is invalid and cannot be played." The file is only about a minute long and is a .mp4 file. I don't have a problem playing it with MPMoviePlayer, it just won't save. Here is the code:

NSString *path = [[NSBundle mainBundle] pathForResource:@"videoFile" ofType:@"mp4"];
UISaveVideoAtPathToSavedPhotosAlbum(path, self, @selector(status:didFinishSavingWithError:contextInfo), nil);

此方法不适用于iPad吗?它说SavedPhotosAlbum。这是否意味着我将不得不通过照片应用程序查看它,或者只是方法的名称,它将在视频应用程序中?如果你可以帮我解决这个问题,我将不胜感激。

Is this method not designed to work for the iPad? It says "SavedPhotosAlbum". Does that mean I will have to go through the photos app to view it, or is that just the name of the method and it will be in the Videos app? If you could help me work this out, I would greatly appreciate it.

推荐答案

只要UIVideoAtPathIsCompatibleWithSavedPhotosAlbum()返回它就应该有效真正。但是,之前我遇到过这个问题,似乎有更好的运气创建和ALAssetsLibrary对象,然后使用方法:

It should work as long as UIVideoAtPathIsCompatibleWithSavedPhotosAlbum() returns true. However, I've had this issue before and seemed to have better luck creating and ALAssetsLibrary object and then using the method:

- (void)writeVideoAtPathToSavedPhotosAlbum:(NSURL *)videoPathURL completionBlock:(ALAssetsLibraryWriteVideoCompletionBlock)completionBlock

我没试过将其编辑为通用或100%便携式。我刚刚抓住了我为保存视频而编写的代码,为您提供了使用ALAssetLibrary的良好起点:

I didn't try to edit this to be generic or 100% portable. I just grabbed the code I wrote for saving videos to give you a good starting point for using ALAssetLibrary instead:

- (void) saveVideoFile:(NSString *)fullpathVideoFile completionTarget:(id)theCompletionTarget action:(SEL)theCompletionAction context:(id)theContext
    {
    writeFailed = NO;

    completionTarget = theCompletionTarget;
    completionAction = theCompletionAction;
    completionContext = theContext;

    //  ALAssetsLibraryWriteVideoCompletionBlock
    //
    void (^completionBlock)(NSURL *, NSError *) = ^(NSURL *assetURL, NSError *error)
        {
        if ( error != nil )
            {
            writeFailed = YES;
            }

        writingToLibrary = NO;

        [self notifyCompletionTarget];
        };


    // clean up from previous calls
    //  
    if ( assetURL != nil )
        {
        [assetURL release];
        assetURL = nil;
        }

    if ( assetFullPathName != nil )
        {
        [assetFullPathName release];
        assetFullPathName = nil;
        }

    writingToLibrary = YES;


    // make sure we have a good file
    //
    if ( [[NSFileManager defaultManager] fileExistsAtPath:fullpathVideoFile] == NO)
        {
        writingToLibrary = NO;
        writeFailed = YES;
        [self notifyCompletionTarget];
        return;
        }


    // set assetURL for sending to the library
    //
    assetFullPathName = [[NSMutableString alloc] initWithCapacity:(NSUInteger)1024];
    [assetFullPathName setString:fullpathVideoFile];

    assetURL = [[NSURL alloc] initFileURLWithPath:assetFullPathName isDirectory:NO];


    // Use possible alternative method if this method doesn't want to work
    //
    if ( [library videoAtPathIsCompatibleWithSavedPhotosAlbum:assetURL]==NO )
        {
        if ( UIVideoAtPathIsCompatibleWithSavedPhotosAlbum( assetFullPathName ) )
            {
            UISaveVideoAtPathToSavedPhotosAlbum( assetFullPathName, self, @selector(video:didFinishSavingWithError:contextInfo:), nil );
            }
        else
            {
            writingToLibrary = NO;
            writeFailed = YES;
            [self notifyCompletionTarget];
            }

        return;
        }


    // Write the video to the library
    //
    [library writeVideoAtPathToSavedPhotosAlbum:assetURL completionBlock:completionBlock];
    }

这篇关于将视频保存到iPad视频应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆