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

最佳答案

我最终使用ALAssetsLibrary并创建了一个URL(assetURL)数组,用于存储相册中视频的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.

关于iphone - 在“照片”中播放相册中的电影,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10657617/

10-11 14:06