我想从 ipad 视频库中获取所有视频列表并显示在表格视图控制器中,然后在应用程序中播放。可能吗?
2 回答
This Developer.apple documentation explains how to use an image picker controller and delegate for browsing and choosing saved pictures and movies. If the standard media browsing UI does not suit your needs, you can create a fully custom solution with UIKit and the Assets Library framework
Media types To specify whether the image picker controller displays saved movies, still images, or both, set the mediaTypes property to an array containing identifiers for the desired types. The valid values for elements of the array are kUTTypeImage and kUTTypeMovie.
Presenting the media browser interface full screen on iPhone or iPod touch
-(BOOL) startMediaBrowserFromViewController: (UIViewController*) controller
usingDelegate: (id <UIImagePickerControllerDelegate,
UINavigationControllerDelegate>) delegate {
if (([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeSavedPhotosAlbum] == NO)
|| (delegate == nil)
|| (controller == nil))
return NO;
UIImagePickerController *mediaUI = [[UIImagePickerController alloc] init];
mediaUI.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
// Displays saved pictures and movies, if both are available, from the
// Camera Roll album.
mediaUI.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];
// Hides the controls for moving & scaling pictures, or for
// trimming movies. To instead show the controls, use YES.
mediaUI.allowsEditing = NO;
mediaUI.delegate = delegate;
[controller presentModalViewController: mediaUI animated: YES];
return YES;
}
- (IBAction) showSavedMediaBrowser {
[self startMediaBrowserFromViewController: self
usingDelegate: self];
}
Delegate method for picked media
- (void) imagePickerController: (UIImagePickerController *) picker
didFinishPickingMediaWithInfo: (NSDictionary *) info {
}
对的,这是可能的。
您可以使用以下代码获取所有视频的资产 url:
NSMutableArray* assetURLs = [[NSMutableArray alloc] init];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
void (^assetEnumerator)( ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop)
{
if(result != nil)
{
if([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo])
{
[assetURLs addObject:[result valueForProperty:ALAssetPropertyURLs]];
NSLog(@"result is:%@",result);
NSURL *url= (NSURL*) [[result defaultRepresentation]url];
[library assetForURL:url
resultBlock:^(ALAsset *asset)
{
// do stuff here
}
failureBlock:^(NSError *error){ NSLog(@"test:Fail"); } ];
}
}
};
您可以使用资产 url 来获取该视频。之后创建视频的缩略图并将其显示在 tableView 中。您还可以使用资产 url 播放视频:
资产网址将如下所示:assets-library://asset/asset.m4v?id=100&ext=m4v
NSString *urlAddress = @"assets-library://asset/asset.m4v?id=100&ext=m4v";
NSURL *movieURL= [NSURL URLWithString:urlAddress];
MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] initWithContentURL: movieURL];
更多参考: