如何从相册中访问视频并播放它们? [英] How to access videos from Photo Album and play them?

查看:278
本文介绍了如何从相册中访问视频并播放它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码用于将视频保存到相册。

Following code used to save videos to photo Album.

else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
    {

        NSString *sourcePath = [[info objectForKey:@"UIImagePickerControllerMediaURL"]relativePath]; 
        UISaveVideoAtPathToSavedPhotosAlbum(sourcePath,nil,nil,nil);
    }

如果是视频我想播放它,如果它是我希望的图像显示它。

If it is video i want to play it if it is image i want to display it.

帮助我。

推荐答案

在你的 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 方法,你可以获取视频的网址并播放它:

In your - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info method, you can get the url of the video and play it:

@property (nonatomic,retain) MPMoviePlayerController *moviePlayerController;

if ([mediaType isEqualToString:@"public.movie"])
{
    NSURL *aURL    = [info objectForKey:UIImagePickerControllerMediaURL];//get the url
    // and init the video player using this url
    _moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:aURL];
    [self.view _moviePlayerController.view];
    _moviePlayerController.useApplicationAudioSession = NO;
    _moviePlayerController.fullscreen = YES;
    [_moviePlayerController play];
}

当然,你必须输入 MediaPlayer.framework

编辑:大多数Cocoa项目现在使用 arc ,所以上面的代码要求你自己保留 MPMoviePlayerController 实例(如这个答案)。

A majority of Cocoa projects now use arc, so the above code would require you retain the MPMoviePlayerController instance yourself (as mentioned in this answer).

这篇关于如何从相册中访问视频并播放它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆