2010-12-20 189 views
1

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

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

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

回答

1

只要UIVideoAtPathIsCompatibleWithSavedPhotosAlbum()返回true,它就会工作。但是,我有这个问题之前,似乎有更好的运气创建和ALAssetsLibrary对象,然后使用方法:

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

我没有尝试编辑这是一般的或100%的便携性。我只是抓住了我写的用于保存视频,让您使用ALAssetLibrary,而不是一个很好的起点代码:

- (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]; 
    } 
+0

感谢您的回复。当我运行UIVideoAtPathIsCompatibleWithSavedPhotosAlbum()时,在控制台上出现“电影无法播放”的错误信息。我没有正确设置路径吗?这是一个我拖放到我的xcode项目中的文件。我不知道为什么它不能得救。它确实发挥与MPMoviePlayer罚款 – Brian 2010-12-20 21:09:10

+0

该文档很少有关为什么这个函数会失败。但是,如果您尝试使用AVAssetLayer方法,您会得到更详细的信息,如果它仍然错误。我已经更新了答案,以包含我的保存视频功能 – 2010-12-20 21:28:18

+0

我有点不知所措的代码。我对iPhone/iPad的开发经验不太了解。所以我不确定如何实现ALAssetsLibrary。我添加了这个框架并且稍微玩了一下你的代码,但是我无法弄清楚。明天我将不得不再看看它。同时,如果有人有另一种方法来解决这个问题,让我知道。谢谢。 – Brian 2010-12-20 22:21:32

4

下面是我的一个项目的一些简单的工作代码应电影/视频保存到相册如果它存在于下面的filePathString并且与设备的相册兼容。我猜测在输入的文件路径中没有文件,或者(更可能)电影大小和格式与iPad/iPhone的相册不兼容。 See the question about video formats compatible with iOS device photo albums for more details.

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; 
// don't forget to link to the AssetsLibrary framework 
// and also #import <AssetsLibrary/AssetsLibrary.h> 

NSString *filePathString = [[NSBundle mainBundle] pathForResource:@"videoFile" ofType:@"mp4"]; 
NSURL *filePathURL = [NSURL fileURLWithPath:filePathString isDirectory:NO]; 
if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:filePathURL]) { 
    [library writeVideoAtPathToSavedPhotosAlbum:filePathURL completionBlock:^(NSURL *assetURL, NSError *error){ 
     if (error) { 
      // TODO: error handling 
     } else { 
      // TODO: success handling 
     } 
    }]; 
} 
[library release];