0

如何从“照片”的相册播放电影?我不想使用 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];
4

1 回答 1

1

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

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.
于 2012-05-24T20:30:03.090 回答