如何获得iPhone中所有视频文件的列表 [英] How to get the list of all video files in iPhone

查看:313
本文介绍了如何获得iPhone中所有视频文件的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要获取存储在iPhone内部(录制和iPod)的所有视频文件的列表。我想显示我的应用程序中的所有视频文件。

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.

我有一个 TableViewController 文件从iphone在我的应用程序。

I have a TableViewController and want to show all video file from iphone in my application.

如何获取所有视频文件的列表?

How can I get a list of all the video files?

推荐答案

p>您必须使用 assetLibraries 才能尝试此代码: -

You have to use assetLibraries Try this code :-

- (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视频列表。

This code will give u list of videos of your iPhone.

它可以帮助你谢谢:)

这篇关于如何获得iPhone中所有视频文件的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆