视频播放直接来自画廊

时间:2014-06-17 13:40:48

标签: ios alassetslibrary

我想直接从画廊播放视频,我已经编写了这段代码,我可以从中获取图像和视频网址,但我无法自动播放视频,任何代码或任何帮助表示赞赏。

    ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];

    [library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
        if (group) {
            [group setAssetsFilter:[ALAssetsFilter allVideos]];
            [group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop){
                if (asset){

                    NSDictionary *meta = [[asset defaultRepresentation] metadata];

                    if ([[asset valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo]) {
                        // asset is a video
                        NSLog(@"See Asset: %@", asset);




                    }

//                    NSLog(@"%@",asset);
//                    NSLog(@"%@",meta);

                }
            }];
        }
    } failureBlock:^(NSError *error) {
        NSLog(@"error enumerating AssetLibrary groups %@\n", error);
    }];

1 个答案:

答案 0 :(得分:0)

在项目中包含MediaPlayer框架,然后在要查看视频的视图控制器中包含:

#import <MediaPlayer/MediaPlayer.h>

@interface MyViewController ()
@property (strong, nonatomic) MPMoviePlayerController *mp;
@end

然后,像这样的东西来初始化它并将它添加到你的层次结构中:

- (void)playUrl:(NSURL *)contentURL {

    MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:contentURL];
    player.view.frame = // some CGRect here that fits your view

    [player prepareToPlay];
    [self.view addSubview:player.view];
    self.mp = player;
}