2010-12-20 247 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];