在“照片”中播放专辑中的电影

时间:2012-05-18 18:05:39

标签: iphone ios ipad

如何从“照片”的相册中播放电影?我不想使用UIImagePickerController浏览电影,但我希望能够指定电影的名称(或某种id),如下面的代码所示。下面的代码在本地播放视频。如何修改照片应用程序中存储的名为“视频”的相册中播放视频的路径?非常感谢您的帮助。

NSString *url = [[NSBundle mainBundle]

                     pathForResource:@"Movie1"

                     ofType:@"MOV"];

MPMoviePlayerViewController *playerViewController =

    [[MPMoviePlayerViewController alloc]

     initWithContentURL:[NSURL fileURLWithPath:url]];

[[NSNotificationCenter defaultCenter]

     addObserver:self

     selector:@selector(movieFinishedCallback:)

     name:MPMoviePlayerPlaybackDidFinishNotification

     object:[playerViewController moviePlayer]];

[playerViewController.view setFrame: self.view.bounds];
[self.view addSubview:playerViewController.view];

MPMoviePlayerController *player = [playerViewController moviePlayer];

[player play];

[playerViewController release];

1 个答案:

答案 0 :(得分:1)

我最终使用了ALAssetsLibrary并创建了一个urls(assetURLs)数组,用于存储相册中视频的网址。这是代码,以防它可能对某人有帮助。

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

ALAssetsLibrary *library = [[ALAssetsLibrary alloc]init];
void (^assetEnumerator)( ALAsset *, NSUInteger , BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop)
{
    if(result != NULL)
    {
        NSLog(@"Asset: %@", result);
        //[assets addObject:result];
        if(![assetURLs containsObject:[result valueForProperty:ALAssetPropertyURLs]]) {
            if([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo]) {
                [assetURLs addObject:[result valueForProperty:ALAssetPropertyURLs]];
                [assets addObject:result];
            }
        }

    }
};    
void (^assetGroupEnumerator)( ALAssetsGroup *, BOOL *) =  ^(ALAssetsGroup *group, BOOL *stop)
{
    if(group != nil)
    {
        [assetGroups addObject:group];
        NSLog(@"GROUP: %@", group);
        [group enumerateAssetsUsingBlock:assetEnumerator];
    }      
};    
void(^ErrorBlock)(NSError*)=^(NSError *error)
{
    NSLog(@"Failure");
};    


[library enumerateGroupsWithTypes:ALAssetsGroupAlbum
                       usingBlock:assetGroupEnumerator
                     failureBlock: ErrorBlock
 ];  

然后,按如下方式初始化MPMoviePlayerViewController对象:

MPMoviePlayerViewController *playerViewController =[[MPMoviePlayerViewController alloc] initWithContentURL:[[[assets objectAtIndex:counter defaultRepresentation] url]]; // counter is incremented to play all videos or set whatever way to play what you want.