Strange behavior: save video recorded within app?

Posted by Josue Espinosa on Stack Overflow See other posts from Stack Overflow or by Josue Espinosa
Published on 2013-10-20T21:52:24Z Indexed on 2013/10/20 21:53 UTC
Read the original article Hit count: 218

I allow the user to record a video within my app, then later play it again. When a user records a video, I save the URL of the video, then play the video later from the saved URL. I save the video both in the Photos app and in my app. If I delete the video within the photos app, it still plays. After about 7 days, the video gets deleted. I think I am saving in my tmp directory, but i'm not sure. Here is what I am doing:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {


    NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
    [self dismissViewControllerAnimated:YES completion:nil];
    // Handle a movie capture
    if (CFStringCompare ((__bridge_retained CFStringRef) mediaType, kUTTypeMovie, 0) == kCFCompareEqualTo) {
        NSString *moviePath = [NSString stringWithFormat:@"%@",[[info objectForKey:UIImagePickerControllerMediaURL] path]];
        NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
        NSData *videoData = [NSData dataWithContentsOfURL:videoURL];
        _justRecordedVideoURL = [NSString stringWithFormat:@"%@",videoURL];

        AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
        _managedObjectContext = [appDelegate managedObjectContext];
        Video *video = [NSEntityDescription insertNewObjectForEntityForName:@"Video" inManagedObjectContext:_managedObjectContext];

        [video setVideoData:videoData];
        [video setVideoURL:[NSString stringWithFormat:@"%@",videoURL]];

        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        dateFormatter.dateStyle = NSDateFormatterLongStyle;
        [dateFormatter setDateStyle:NSDateFormatterLongStyle];
        NSDate *date = [dateFormatter dateFromString:[dateFormatter stringFromDate:[NSDate date]]];
        NSString *dateAdded = [dateFormatter stringFromDate:date];
        [video setDate_recorded:dateAdded];

        if(_currentAthlete != nil){
        [video setWhosVideo:_currentAthlete];
        }

        NSError *error = nil;
        if(![_managedObjectContext save:&error]){
            //handle dat error
        }


        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *tempPath = [documentsDirectory stringByAppendingFormat:@"/vid1.mp4"];

        BOOL success = [videoData writeToFile:tempPath atomically:NO];

        if(success == FALSE){
            NSLog(@"Video was not successfully saved.");
        }

        if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(moviePath)) {
            UISaveVideoAtPathToSavedPhotosAlbum(moviePath, self,
                                                @selector(video:didFinishSavingWithError:contextInfo:), nil);

        }
    }
}

Am I saving it incorrectly? When I go to play the video, it works fine, after a couple days the video will play without audio, then eventually it will be gone. Any ideas why?

© Stack Overflow or respective owner

Related posts about ios

Related posts about objective-c