I want to get the list of all video files which are stored in iPhone internally (recorded and iPod). I want to show all the video files in my application.
我想获取内部存储在iPhone中的所有视频文件列表(录制和iPod)。我想在我的应用程序中显示所有视频文件。
I have a TableViewController
and want to show all video file from iphone in my application.
我有一个TableViewController,并希望在我的应用程序中显示来自iphone的所有视频文件。
How can I get a list of all the video files?
如何获取所有视频文件的列表?
2 个解决方案
#1
11
You have to use assetLibraries Try this code :-
你必须使用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];
}
This code will give u list of videos of your iPhone.
此代码将为您提供iPhone的视频列表。
It may help you Thankss :)
它可能会帮助你感谢:)
#2
4
Get List of all Video and Thumbnails
获取所有视频和缩略图的列表
With the help of above answer I got it working..
在上面的回答的帮助下,我得到了它的工作..
Thanks to @Nikhil Bansal,
感谢@Nikhil Bansal,
it helped me, but still it required couple of hours to make the code executable as he is missing few things in his answer
它帮助了我,但是仍然需要几个小时来使代码可执行,因为他在答案中缺少一些东西
So I would like to share my full working code
所以我想分享我的完整工作代码
1.just add frameworks AssetsLibrary, AVFoundation and MediaPlayer.
1.just添加框架AssetsLibrary,AVFoundation和MediaPlayer。
2.AssetBrowserItem.h and AssetBrowserItem.m here
2.AssetBrowserItem.h和AssetBrowserItem.m在这里
3.use below code to get list of all videos from ios device lib
3.使用以下代码获取ios设备库中所有视频的列表
4.run app and see Log for videos details
4.运行应用程序并查看日志视频详细信息
#import "HomeViewController.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import <MediaPlayer/MediaPlayer.h>
#import <AVFoundation/AVFoundation.h>
#import "AssetBrowserItem.h"
@interface HomeViewController ()
@property (nonatomic, strong) ALAssetsLibrary *assetsLibrary;
@property (nonatomic, strong) NSURL *videoURL;
@property (nonatomic, strong) MPMoviePlayerController *mpVideoPlayer;
@property (nonatomic, strong) NSMutableArray *videoURLArray;
@property (nonatomic, strong) NSMutableArray *assetItems;
@property (nonatomic, strong) NSMutableDictionary *dic;
@end
@implementation HomeViewController
@synthesize assetsLibrary, assetItems,dic;
@synthesize videoURL,videoURLArray, mpVideoPlayer;
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - Show Video List Methods
- (IBAction)showVideoList:(id)sender
{
[self buildAssetsLibrary];
}
- (void)buildAssetsLibrary
{
assetsLibrary = [[ALAssetsLibrary alloc] init];
ALAssetsLibrary *notificationSender = nil;
videoURLArray = [[NSMutableArray alloc] init];
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];
}
- (void)assetsLibraryDidChange:(NSNotification*)changeNotification
{
[self updateAssetsLibrary];
}
- (void)updateAssetsLibrary
{
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];
videoURL = [[asset valueForProperty:ALAssetPropertyURLs] valueForKey:uti];
mpVideoPlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
NSString *title = [NSString stringWithFormat:@"%@ %lu", NSLocalizedString(@"Video", nil), [assetItems count]+1];
[self performSelector:@selector(imageFromVideoURL)];
[dic setValue:title forKey:@"VideoTitle"];//kName
[dic setValue:videoURL forKey:@"VideoUrl"];//kURL
AssetBrowserItem *item = [[AssetBrowserItem alloc] initWithURL:videoURL title:title];
[assetItems addObject:item];
[videoURLArray addObject:dic];
NSLog(@"Video has info:%@",videoURLArray);
}
NSLog(@"Values of dictonary==>%@", dic);
//NSLog(@"assetItems:%@",assetItems);
NSLog(@"Videos Are:%@",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
{
UIImage *image = nil;
AVAsset *asset = [[AVURLAsset alloc] initWithURL: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:@"ImageThumbnail"];//kImage
NSLog(@"Values of dictonary==>%@", dic);
NSLog(@"Videos Are:%@",videoURLArray);
CGImageRelease(halfWayImage);
}
return image;
}
@end
#1
11
You have to use assetLibraries Try this code :-
你必须使用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];
}
This code will give u list of videos of your iPhone.
此代码将为您提供iPhone的视频列表。
It may help you Thankss :)
它可能会帮助你感谢:)
#2
4
Get List of all Video and Thumbnails
获取所有视频和缩略图的列表
With the help of above answer I got it working..
在上面的回答的帮助下,我得到了它的工作..
Thanks to @Nikhil Bansal,
感谢@Nikhil Bansal,
it helped me, but still it required couple of hours to make the code executable as he is missing few things in his answer
它帮助了我,但是仍然需要几个小时来使代码可执行,因为他在答案中缺少一些东西
So I would like to share my full working code
所以我想分享我的完整工作代码
1.just add frameworks AssetsLibrary, AVFoundation and MediaPlayer.
1.just添加框架AssetsLibrary,AVFoundation和MediaPlayer。
2.AssetBrowserItem.h and AssetBrowserItem.m here
2.AssetBrowserItem.h和AssetBrowserItem.m在这里
3.use below code to get list of all videos from ios device lib
3.使用以下代码获取ios设备库中所有视频的列表
4.run app and see Log for videos details
4.运行应用程序并查看日志视频详细信息
#import "HomeViewController.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import <MediaPlayer/MediaPlayer.h>
#import <AVFoundation/AVFoundation.h>
#import "AssetBrowserItem.h"
@interface HomeViewController ()
@property (nonatomic, strong) ALAssetsLibrary *assetsLibrary;
@property (nonatomic, strong) NSURL *videoURL;
@property (nonatomic, strong) MPMoviePlayerController *mpVideoPlayer;
@property (nonatomic, strong) NSMutableArray *videoURLArray;
@property (nonatomic, strong) NSMutableArray *assetItems;
@property (nonatomic, strong) NSMutableDictionary *dic;
@end
@implementation HomeViewController
@synthesize assetsLibrary, assetItems,dic;
@synthesize videoURL,videoURLArray, mpVideoPlayer;
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - Show Video List Methods
- (IBAction)showVideoList:(id)sender
{
[self buildAssetsLibrary];
}
- (void)buildAssetsLibrary
{
assetsLibrary = [[ALAssetsLibrary alloc] init];
ALAssetsLibrary *notificationSender = nil;
videoURLArray = [[NSMutableArray alloc] init];
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];
}
- (void)assetsLibraryDidChange:(NSNotification*)changeNotification
{
[self updateAssetsLibrary];
}
- (void)updateAssetsLibrary
{
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];
videoURL = [[asset valueForProperty:ALAssetPropertyURLs] valueForKey:uti];
mpVideoPlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
NSString *title = [NSString stringWithFormat:@"%@ %lu", NSLocalizedString(@"Video", nil), [assetItems count]+1];
[self performSelector:@selector(imageFromVideoURL)];
[dic setValue:title forKey:@"VideoTitle"];//kName
[dic setValue:videoURL forKey:@"VideoUrl"];//kURL
AssetBrowserItem *item = [[AssetBrowserItem alloc] initWithURL:videoURL title:title];
[assetItems addObject:item];
[videoURLArray addObject:dic];
NSLog(@"Video has info:%@",videoURLArray);
}
NSLog(@"Values of dictonary==>%@", dic);
//NSLog(@"assetItems:%@",assetItems);
NSLog(@"Videos Are:%@",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
{
UIImage *image = nil;
AVAsset *asset = [[AVURLAsset alloc] initWithURL: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:@"ImageThumbnail"];//kImage
NSLog(@"Values of dictonary==>%@", dic);
NSLog(@"Videos Are:%@",videoURLArray);
CGImageRelease(halfWayImage);
}
return image;
}
@end