1

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);

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.

2 Answers 2

4

Here's some simpler working code from one of my projects that should save a movie / video to the photo album if it exists at the filePathString below and is compatible with the photo album for the device. I'm guessing that there either was no file at the file path entered or (more likely) the movie size and format was not compatible with the photo album for the 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];
1

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

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];
    }
4
  • Thanks for the reply. When I ran UIVideoAtPathIsCompatibleWithSavedPhotosAlbum() I got an error on the console saying "Movie could not be played." Am I not setting the path correctly? It's a file that I drag-and-dropped into my xcode project. I don't know any other reason why it cant be saved. It does play fine with MPMoviePlayer
    – Brian
    Dec 20, 2010 at 21:09
  • The docs are minimal about why this function would fail. However, if you try the AVAssetLayer approach you will get more detailed information if it does still error. I've updated the answer to include a my function for saving videos Dec 20, 2010 at 21:28
  • I'm a bit overwhelmed with your code. I am not too experienced with iPhone/iPad development. So I'm not real sure how to implement the ALAssetsLibrary. I added the framework and played around with your code a bit but I wasn't able to figure it out. I will have to look into it a bit more tomorrow. In the meantime, if anyone has another way to fix this problem, let me know. Thanks.
    – Brian
    Dec 20, 2010 at 22:21
  • I found this post: stackoverflow.com/questions/3618054/… and changed the "writeImageToSavedPhotosAlbum" to "writeVideoAtPathToSavedPhotosAlbum". I tried to save my file again, but I am getting an error saying "Invalid data... There was a problem writing this asset because the data is invalid and cannot be viewed or played." I converted the original mp4 file to a m4v file and it still didn't work. Any other pointers?
    – Brian
    Dec 21, 2010 at 16:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.