我想获取内部存储在iPhone中的所有视频文件列表(录制和iPod).我想在我的应用程序中显示所有视频文件.
我有一个TableViewController,并希望在我的应用程序中显示来自iphone的所有视频文件.
如何获取所有视频文件的列表?
解决方法
你必须使用assetLibraries试试这个代码: –
- (void)updateAssetsLibrary
{
loadImgView.hidden = NO;
[spinner startAnimating];
//selectVideoBtn .userInteractionEnabled = NO;
assetItems = [NSMutableArray arrayWithCapacity:0];
ALAssetsLibrary *assetLibrary = AssetsLibrary;
[assetLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group,BOOL *stop)
{
if (group)
{
[group setAssetsFilter:[ALAssetsFilter allVideos]];
[group enumerateAssetsUsingBlock:^(ALAsset *asset,NSUInteger index,BOOL *stop)
{
if (asset)
{
dic = [[NSMutableDictionary alloc] init];
ALAssetRepresentation *defaultRepresentation = [asset defaultRepresentation];
Nsstring *uti = [defaultRepresentation UTI];
appDelegate.videoURL = [[asset valueForProperty:ALAssetPropertyURLs] valueForKey:uti];
mpVideoPlayer = [[MPMoviePlayerController alloc] initWithContentURL:appDelegate.videoURL];
Nsstring *title = [Nsstring stringWithFormat:@"%@ %i",NSLocalizedString(@"Video",nil),[assetItems count]+1];
[self performSelector:@selector(imageFromVideoURL)];
[dic setValue:title forKey:kName];
[dic setValue:appDelegate.videoURL forKey:kURL];
AssetbrowserItem *item = [[AssetbrowserItem alloc] initWithURL:appDelegate.videoURL title:title];
[assetItems addobject:item];
[appDelegate.videoURLArray addobject:dic];
NSLog(@"Video has info:%@",appDelegate.videoURLArray);
}
NSLog(@"Values of dictonary==>%@",dic);
//NSLog(@"assetItems:%@",assetItems);
NSLog(@"Videos Are:%@",appDelegate.videoURLArray);
} ];
}
// group == nil signals we are done iterating.
else
{
dispatch_async(dispatch_get_main_queue(),^{
//[self updatebrowserItemsAndSignalDelegate:assetItems];
loadImgView.hidden = NO;
[spinner stopAnimating];
[loadImgView removeFromSuperview];
//selectVideoBtn .userInteractionEnabled = YES;
});
}
}
failureBlock:^(NSError *error)
{
NSLog(@"error enumerating AssetLibrary groups %@\n",error);
}];
}
- (UIImage *)imageFromVideoURL
{
// result
UIImage *image = nil;
// AVAssetimageGenerator
AVAsset *asset = [[AVURLAsset alloc] initWithURL:appDelegate.videoURL options:nil];;
AVAssetimageGenerator *imageGenerator = [[AVAssetimageGenerator alloc] initWithAsset:asset];
imageGenerator.appliesPreferredTrackTransform = YES;
// calc midpoint time of video
Float64 durationSeconds = CMTimeGetSeconds([asset duration]);
CMTime midpoint = CMTimeMakeWithSeconds(durationSeconds/2.0,600);
// get the image from
NSError *error = nil;
CMTime actualTime;
CGImageRef halfWayImage = [imageGenerator copyCGImageAtTime:midpoint actualTime:&actualTime error:&error];
if (halfWayImage != NULL)
{
// cgimage to uiimage
image = [[UIImage alloc] initWithCGImage:halfWayImage];
[dic setValue:image forKey:kImage];
NSLog(@"Values of dictonary==>%@",dic);
NSLog(@"Videos Are:%@",appDelegate.videoURLArray);
CGImageRelease(halfWayImage);
}
return image;
}
- (void)AssetsLibraryDidChange:(NSNotification*)changeNotification
{
[self updateAssetsLibrary];
}
- (void)buildAssetsLibrary
{
AssetsLibrary = [[ALAssetsLibrary alloc] init];
ALAssetsLibrary *notificationSender = nil;
Nsstring *minimumSystemVersion = @"4.1";
Nsstring *systemVersion = [[UIDevice currentDevice] systemVersion];
if ([systemVersion compare:minimumSystemVersion options:NSNumericSearch] != NSOrderedAscending)
notificationSender = AssetsLibrary;
[[NSNotificationCenter defaultCenter] addobserver:self selector:@selector(AssetsLibraryDidChange:) name:ALAssetsLibraryChangednotification object:notificationSender];
[self updateAssetsLibrary];
}
此代码将为您提供iPhone的视频列表.
它可能会帮助你感谢:)