2012-05-25 50 views
7

Tôi muốn nhận danh sách tất cả các tệp video được lưu trữ trong iPhone (được ghi lại và iPod). Tôi muốn hiển thị tất cả các tệp video trong ứng dụng của mình.Cách lấy danh sách tất cả các tệp video trong iPhone

Tôi có một số TableViewController và muốn hiển thị tất cả tệp video từ iphone trong ứng dụng của mình.

Làm cách nào để có danh sách tất cả các tệp video?

+1

Có điều này có thể nhưng trước hết hãy cho tôi biết những gì bạn đã thử cho đến nay ?? –

+0

tôi muốn tìm nạp tất cả các tập tin video từ iphone (ghi và các tập tin video khác được lưu trữ trong iphone) – dheeru

Trả lời

11

Bạn phải sử dụng assetLibraries Hãy thử mã này: -

- (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]; 
} 

Mã này sẽ cung cấp cho u danh sách các video của iPhone của bạn.

Nó có thể giúp bạn thankss :)

+0

Nó cũng sẽ cung cấp cho các video từ 'ứng dụng video'? – Maulik

+0

Không, tôi muốn tìm nạp tất cả các tập tin video từ iphone chỉ (ghi và các tập tin video khác được lưu trữ trong iphone) – dheeru

+0

toh nó sẽ cho na !!!! –

3

Nhận Danh sách tất cả các video và Thumbnail

Với sự giúp đỡ của câu trả lời trên, chúng tôi đã nhận nó làm việc ..

Nhờ @Nikhil Bansal,

nó đã giúp tôi, nhưng nó vẫn cần vài giờ để làm cho đoạn code thực thi như ông là mất tích vài điều trong câu trả lời của ông

enter image description here

Vì vậy, tôi muốn chia sẻ mã làm việc đầy đủ của tôi

1.just thêm khung AssetsLibrary, AVFoundationMediaPlayer.

2. AssetBrowserItem.hAssetBrowserItem.mhere

3.Use mã dưới đây để có được danh sách của tất cả các video từ thiết bị ios lib

4.run ứng dụng và xem Log cho video chi tiết

#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 
Các vấn đề liên quan