2013-07-11 39 views
5

Xin chào Tôi đang làm việc Trên video, tôi muốn nhận danh sách tệp video từ thư viện để Hiển thị và phát Video trong ứng dụng của tôi. Ai giúp tôi với.Làm thế nào tôi có thể lấy danh sách tất cả các tệp video từ thư viện trong ios sdk

+0

Bạn có cố gắng bất cứ điều gì? –

+0

Có thể điều này sẽ giúp bạn: http://stackoverflow.com/questions/8301950/how-to-get-the-list-of-all-gallery-images-in-ios – OMK

+0

@NagRaj một số người hỏi bạn. một số người trả lời bạn. plz phát lại. Nếu bạn muốn giúp đỡ về SO. –

Trả lời

10
allVideos = [[NSMutableArray alloc] init]; 
ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init]; 

[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]; 
        NSURL *videoURL = [[asset valueForProperty:ALAssetPropertyURLs] valueForKey:uti]; 
        NSString *title = [NSString stringWithFormat:@"video %d", arc4random()%100]; 
        UIImage *image = [self imageFromVideoURL:videoURL]; 
        [dic setValue:image forKey:@"image"]; 
        [dic setValue:title forKey:@"name"]; 
        [dic setValue:videoURL forKey:@"url"]; 
        [allVideos addObject:dic]; 
       } 
      }]; 
    else 
    { 
    } 
} 
    failureBlock:^(NSError *error) 
{ 
    NSLog(@"error enumerating AssetLibrary groups %@\n", error); 
}]; 
3

Nó sẽ mở thư viện ảnh và chỉ hiển thị nội dung loại phim.

#import <MobileCoreServices/MobileCoreServices.h> 

UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; 
imagePicker.delegate = self; 
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
imagePicker.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie, nil]; 
[self presentModalViewController:imagePicker animated:YES]; 
0

Vui lòng tham khảo liên kết bên dưới. Có thể hữu ích cho bạn

http://www.raywenderlich.com/13418/how-to-play-record-edit-videos-in-ios
Chúc may mắn !!!

+0

Trong khi điều này về lý thuyết có thể trả lời câu hỏi, [nó sẽ là thích hợp hơn] (http: //meta.stackexchange.com/q/8259) để bao gồm các phần thiết yếu của câu trả lời ở đây và cung cấp liên kết để tham khảo. Bạn cũng cần giải thích * cách * nó trả lời câu hỏi. –

0

sử dụng phương pháp này Để chọn video Từ Thư viện.

 //Call this method. 
    [self startMediaBrowserFromViewController: self usingDelegate: self]; 


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

     mediaUI.mediaTypes = [[[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil] autorelease]; 
     mediaUI.allowsEditing = YES; 
     mediaUI.delegate = self; 
     mediaUI.videoMaximumDuration = 60.0; 
     //mediaUI.videoQuality = UIImagePickerControllerQualityTypeLow; 

     [controller presentModalViewController: mediaUI animated: YES]; 
     return YES; 
    } 
3

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

Nhờ @Nikhil, người chia sẻ thông tin here, nó đã giúp tôi, nhưng nó vẫn cần vài giờ để làm cho mã có thể thực thi được vì thiếu một số thứ trong câu trả lời của mình

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 các 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 
0

Thanh toán các bài viết trên blog để lấy toàn bộ tài sản video sử dụng hình ảnh Khung http://iavinashkashyap.blogspot.in/2016/11/get-list-of-all-videos.html

Code:

- (void) {getAllVideoAssets

NSMutableArray *assets = [[NSMutableArray alloc] init]; 
//Fetch all video assets from Photos 
PHFetchResult *assetResults = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeVideo options:nil]; 
//get all assets 
for (PHAsset *asset in assetResults){ 
    NSLog(@"asset type = %zd", asset.mediaType); 
    [assets addObject:asset]; 
} 
self.allVideoslistArray = [[NSMutableArray alloc] init]; 
//create an instance of PHImageManager 
PHImageManager *manager = [PHImageManager defaultManager]; 
for(PHAsset *asset in assets){ 
    //block of code for represent video assets 
    [manager requestAVAssetForVideo:asset options:nil resultHandler:^(AVAsset * _Nullable asset, AVAudioMix * _Nullable audioMix, NSDictionary * _Nullable info) { 
     if ([asset isKindOfClass:[AVURLAsset class]]) { 
      NSURL *url = (NSURL *)[[(AVURLAsset *)asset URL] fileReferenceURL]; 
      UIImage *thumbnail = [self createThunbnailImage:url]; 
      [self.allVideoslistArray addObject:@{@"VideoUrl":url,@"ThumbnailImage":thumbnail, @"VideoAsset":asset}]; 
     } 
    }]; 
}//end for loop 

}

+0

Sẽ tuyệt vời nếu bạn cung cấp phiên bản Swift 3? –

+0

@Anura Tôi sẽ thực hiện tương tự trong Swift. – avi

Các vấn đề liên quan