2012-05-18 44 views
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]; 

回答

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.