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

2023-11-26

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

我有一个TableViewController并想在我的应用程序中显示 iPhone 上的所有视频文件。

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


你必须使用资产库试试这个代码:-

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

它可能对你有帮助谢谢:)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何获取iPhone中所有视频文件的列表 的相关文章

随机推荐