Reputation: 469
How do I play a movie from an album from "Photos"? I don't want to use UIImagePickerController to browse the movie, but I would like to be able to specify the name of the movie (or some sort of id) as in the following code to play it. The code below plays a video locally. How do I modify the path to play a video from the album called "Video" stored in the Photos app?Thanks so much for your help.
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];
Upvotes: 0
Views: 575
Reputation: 469
I ended up using ALAssetsLibrary and created an array of urls (assetURLs) that stores the urls of the videos from the photo album. Here is the code in case it might be helpful to someone.
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
];
Then, initialize the MPMoviePlayerViewController object as follows:
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.
Upvotes: 1