7

iPhone内部(録画・iPod)に保存されている全ての動画ファイルの一覧を取得したいです。アプリケーションですべてのビデオ ファイルを表示したいと考えています。

TableViewControllerアプリケーションで iphone からのすべてのビデオ ファイルを表示したいと考えています。

すべてのビデオ ファイルのリストを取得するにはどうすればよいですか?

4

2 に答える 2

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 のビデオのリストを表示します。

それはあなたを助けるかもしれませんありがとう:)

于 2012-05-25T05:09:42.140 に答える
4

すべてのビデオとサムネイルのリストを取得する

上記の回答の助けを借りて、私はそれを機能させました..

@Nikhil Bansal に感謝します。

それは私を助けましたが、彼の答えにはいくつか欠けているため、コードを実行可能にするのに数時間かかりました

ここに画像の説明を入力

だから私は私の完全な作業コードを共有したいと思います

1. フレームワークAssetsLibraryAVFoundation、およびMediaPlayerを追加するだけです。

2. AssetBrowserItem.hAssetBrowserItem.m はこちら

3. 以下のコードを使用して、iOS デバイス ライブラリからすべてのビデオのリストを取得します。

4.アプリを実行し、ビデオの詳細についてはログを参照してください

#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
于 2015-02-05T12:18:07.470 に答える