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

Posted

技术标签:

【中文标题】在 iOS 7 中从照片应用播放视频【英文标题】:Play video from photos app in iOS 7 【发布时间】:2013-09-29 03:00:01 【问题描述】:

在我的应用程序中,我允许用户录制视频并将视频保存到默认照片应用程序。我将视频的 URL 保存到 Core Data 中,并想知道是否有任何方法可以从保存的 URL 中提取 AVPlayer,而不提取 UIImagePicker。这是我用来录制和播放视频的工具:

记录.m

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

        
    

play.m

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

【问题讨论】:

【参考方案1】:

其实很简单,只需获取 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];

【讨论】:

以上是关于在 iOS 7 中从照片应用播放视频的主要内容,如果未能解决你的问题,请参考以下文章

如何确定 iOS 设备的视频播放能力?

如何在 android 中从 react 本机应用程序播放 mov 视频?

在 VideoView 中从暂停状态播放视频

在 iOS 7 横向模式下播放视频

如何在 iOS 中播放视频 PHAsset?

使用 AVPlayer 播放的视频在 swift3 中从其容器视图中消失