在iOS 7中从照片应用程序播放视频

在iOS 7中从照片应用程序播放视频,ios,objective-c,cocoa-touch,avplayer,Ios,Objective C,Cocoa Touch,Avplayer,在我的应用程序中,我允许用户录制视频,并将视频保存到默认的照片应用程序中。我将视频的URL保存到核心数据中,并想知道是否有任何方法可以从保存的URL中拉出AVPlayer,而无需拉出UIImagePicker。以下是我用来录制和播放视频的内容: 记录 -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { NSSt

在我的应用程序中,我允许用户录制视频,并将视频保存到默认的照片应用程序中。我将视频的URL保存到核心数据中,并想知道是否有任何方法可以从保存的URL中拉出
AVPlayer
,而无需拉出
UIImagePicker
。以下是我用来录制和播放视频的内容:

记录

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
    [self dismissModalViewControllerAnimated:NO];
    // Handle a movie capture
    if (CFStringCompare ((__bridge_retained CFStringRef) mediaType, kUTTypeMovie, 0) == kCFCompareEqualTo) {
        NSString *moviePath = [[info objectForKey:UIImagePickerControllerMediaURL] path];

        NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
        NSData *videoData = [NSData dataWithContentsOfURL:videoURL];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *tempPath = [documentsDirectory stringByAppendingFormat:@"/vid1.mp4"];

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

        NSLog(@"%hhd",success);

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

        }
    }
}

-(BOOL)startMediaBrowserFromViewController:(UIViewController*)controller usingDelegate:(id )delegate {
    // 1 - Validations
    if (([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum] == NO)
        || (delegate == nil)
        || (controller == nil)) {
        return NO;
    }
    // 2 - Get image picker
    UIImagePickerController *mediaUI = [[UIImagePickerController alloc] init];
    mediaUI.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    mediaUI.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];
    // Hides the controls for moving & scaling pictures, or for
    // trimming movies. To instead show the controls, use YES.
    mediaUI.allowsEditing = YES;
    mediaUI.delegate = delegate;
    // 3 - Display image picker
    [controller presentModalViewController:mediaUI animated:YES];
    return YES;
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    // 1 - Get media type
    NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
    // 2 - Dismiss image picker
    [self dismissModalViewControllerAnimated:NO];
    // Handle a movie capture
    NSURL *assetURL = [info objectForKey:UIImagePickerControllerReferenceURL];

    if (CFStringCompare ((__bridge_retained CFStringRef)mediaType, kUTTypeMovie, 0) == kCFCompareEqualTo) {
        // 3 - Play the video
        MPMoviePlayerViewController *theMovie = [[MPMoviePlayerViewController alloc]
                                                 initWithContentURL:[info objectForKey:UIImagePickerControllerMediaURL]];
        MPMoviePlayerViewController *movie = [[MPMoviePlayerViewController alloc] initWithContentURL:assetURL];

        [self presentMoviePlayerViewControllerAnimated:movie];
        // 4 - Register for the playback finished notification
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMovieFinishedCallback:)
                                                     name:MPMoviePlayerPlaybackDidFinishNotification object:theMovie];
    }
}

其实很简单,只需获取URL并使用它设置MPMoviePlayer

NSURL *assetURL = [NSURL URLWithString:videoSelected.videoURL];
MPMoviePlayerViewController *movie = [[MPMoviePlayerViewController alloc] initWithContentURL:assetURL];
[self presentMoviePlayerViewControllerAnimated:movie];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMovieFinishedCallback:)
                                             name:MPMoviePlayerPlaybackDidFinishNotification object:movie];