如何在自定义表视图中显示ipad库视频

时间:2012-11-02 07:58:50

标签: iphone ios ios5

我想从ipad视频库获取所有视频列表并在表格视图控制器中显示,然后在应用程序内播放。有可能吗?

2 个答案:

答案 0 :(得分:0)

This Developer.apple documentation说明了如何使用图像选择器控制器并委托浏览和选择已保存的图片和电影。如果标准媒体浏览UI不适合您的需求,您可以使用UIKit和资产库框架创建完全自定义的解决方案

媒体类型要指定图像选择器控制器是显示已保存的电影,静止图像还是两者,请将mediaTypes属性设置为包含所需类型标识符的数组。数组元素的有效值是kUTTypeImage和kUTTypeMovie。

在iPhone或iPod touch上全屏显示媒体浏览器界面

 -(BOOL) startMediaBrowserFromViewController: (UIViewController*) controller

               usingDelegate: (id <UIImagePickerControllerDelegate,

                                   UINavigationControllerDelegate>) delegate {



    if (([UIImagePickerController isSourceTypeAvailable:

                 UIImagePickerControllerSourceTypeSavedPhotosAlbum] == NO)

            || (delegate == nil)

            || (controller == nil))

        return NO;



    UIImagePickerController *mediaUI = [[UIImagePickerController alloc] init];

    mediaUI.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;



    // Displays saved pictures and movies, if both are available, from the

    // Camera Roll album.

     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 = NO;



    mediaUI.delegate = delegate;



    [controller presentModalViewController: mediaUI animated: YES];

    return YES;

}

- (IBAction) showSavedMediaBrowser {

    [self startMediaBrowserFromViewController: self

                                    usingDelegate: self];

}

选择媒体的委托方法

- (void) imagePickerController: (UIImagePickerController *) picker

            didFinishPickingMediaWithInfo: (NSDictionary *) info {
}

答案 1 :(得分:0)

是的,这是可能的。

您可以使用以下代码获取所有视频的资源网址:

NSMutableArray* assetURLs = [[NSMutableArray alloc] init];

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

void (^assetEnumerator)( ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop)
{
    if(result != nil)
    {
        if([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo])
        {
            [assetURLs addObject:[result valueForProperty:ALAssetPropertyURLs]];

            NSLog(@"result is:%@",result);
            NSURL *url= (NSURL*) [[result defaultRepresentation]url]; 

            [library assetForURL:url
                     resultBlock:^(ALAsset *asset)
             {
                      // do stuff here
             }
             failureBlock:^(NSError *error){ NSLog(@"test:Fail"); } ]; 
        } 
    }
};

您可以使用资产网址来获取该视频。 之后,创建视频的缩略图并将其显示在tableView中。 您也可以使用资产网址播放视频:

资产网址将如下所示:assets-library://asset/asset.m4v?id=100&ext=m4v

NSString *urlAddress = @"assets-library://asset/asset.m4v?id=100&ext=m4v";
NSURL *movieURL= [NSURL URLWithString:urlAddress];
MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] initWithContentURL: movieURL];

更多参考:

  1. AssetsLibrary
  2. MPMoviePlayerController