2012-05-25 48 views
7

我想獲取iPhone內部(錄製和iPod)中存儲的所有視頻文件的列表。我想在我的應用程序中顯示所有視頻文件。如何獲取iPhone中所有視頻文件的列表

我有一個TableViewController,並希望在我的應用程序中顯示來自iPhone的所有視頻文件。

如何獲得所有視頻文件的列表?

+1

是的,這是可能的,但首先告訴我你到目前爲止嘗試過什麼? –

+0

我想從iPhone取得所有的視頻文件(錄製的和iPhone中存儲的其他視頻文件) – dheeru

回答

11

你必須使用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的視頻列表。

它可以幫助你Thankss :)

+0

它也會給'視頻應用程序'視頻嗎? – Maulik

+0

不,我想從iphone只獲取所有視頻文件(錄製和其他視頻文件存儲在iphone) – dheeru

+0

toh它會給na! –

3

獲取的所有視頻和縮略圖

列表和我得到它的工作上面的回答的幫助..

感謝@Nikhil Bansal,

它幫助我,但它仍然需要幾個小時,使代碼執行,因爲他缺少一些東西在他的回答

enter image description here

所以,我想和大家分享我的全部工作代碼

1.just添加框架AssetsLibraryAVFoundationMediaPlayer的

2. AssetBrowserItem.hAssetBrowserItem.mhere

3.使用下面的代碼獲得的來自iOS設備的所有視頻列表的lib

4.run應用程序,請參閱日誌用於視頻細節

#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 
相關問題