【问题标题】:Save Video to iPad Videos app将视频保存到 iPad 视频应用程序
【发布时间】:2011-05-28 11:29:00
【问题描述】:

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

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

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

【问题讨论】:

    标签: iphone ipad video save


    【解决方案1】:

    这是我的一个项目中的一些更简单的工作代码,如果电影/视频存在于下面的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];
    

    【讨论】:

      【解决方案2】:

      只要 UIVideoAtPathIsCompatibleWithSavedPhotosAlbum() 返回 true,它就应该工作。但是,我之前遇到过这个问题,并且似乎更幸运地创建和 AL​​AssetsLibrary 对象,然后使用该方法:

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

      【讨论】:

      • 感谢您的回复。当我运行 UIVideoAtPathIsCompatibleWithSavedPhotosAlbum() 时,控制台上出现错误提示“无法播放电影”。我没有正确设置路径吗?这是我拖放到我的 xcode 项目中的文件。我不知道它无法保存的任何其他原因。使用 MPMoviePlayer 播放效果很好
      • 有关此功能为何失败的文档很少。但是,如果您尝试 AVAssetLayer 方法,如果仍然出错,您将获得更详细的信息。我已经更新了答案以包含我的保存视频的功能
      • 我对你的代码有点不知所措。我对 iPhone/iPad 的开发经验不是很丰富。所以我不确定如何实现 ALAssetsLibrary。我添加了框架并稍微玩弄了您的代码,但我无法弄清楚。明天我得再研究一下。同时,如果有人有其他方法可以解决此问题,请告诉我。谢谢。
      • 我发现了这篇文章:stackoverflow.com/questions/3618054/… 并将“writeImageToSavedPhotosAlbum”更改为“writeVideoAtPathToSavedPhotosAlbum”。我尝试再次保存我的文件,但我收到一条错误消息,提示“无效数据...写入此资产时出现问题,因为数据无效且无法查看或播放。”我将原始的 mp4 文件转换为 m4v 文件,但仍然无法正常工作。还有其他指针吗?
      猜你喜欢
      • 2015-07-15
      • 2011-07-02
      • 1970-01-01
      • 1970-01-01
      • 2013-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-24
      相关资源
      最近更新 更多