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

前端之家收集整理的这篇文章主要介绍了ios – 如何获取iPhone中所有视频文件的列表前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想获取内部存储在iPhone中的所有视频文件列表(录制和iPod).我想在我的应用程序中显示所有视频文件.

我有一个TableViewController,并希望在我的应用程序中显示来自iphone的所有视频文件.

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

解决方法

你必须使用assetLibraries试试这个代码: –
  1. - (void)updateAssetsLibrary
  2. {
  3. loadImgView.hidden = NO;
  4. [spinner startAnimating];
  5. //selectVideoBtn .userInteractionEnabled = NO;
  6.  
  7. assetItems = [NSMutableArray arrayWithCapacity:0];
  8. ALAssetsLibrary *assetLibrary = assetsLibrary;
  9.  
  10. [assetLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group,BOOL *stop)
  11. {
  12. if (group)
  13. {
  14. [group setAssetsFilter:[ALAssetsFilter allVideos]];
  15. [group enumerateAssetsUsingBlock:^(ALAsset *asset,NSUInteger index,BOOL *stop)
  16. {
  17. if (asset)
  18. {
  19. dic = [[NSMutableDictionary alloc] init];
  20. ALAssetRepresentation *defaultRepresentation = [asset defaultRepresentation];
  21. NSString *uti = [defaultRepresentation UTI];
  22. appDelegate.videoURL = [[asset valueForProperty:ALAssetPropertyURLs] valueForKey:uti];
  23.  
  24. mpVideoPlayer = [[MPMoviePlayerController alloc] initWithContentURL:appDelegate.videoURL];
  25.  
  26. NSString *title = [NSString stringWithFormat:@"%@ %i",NSLocalizedString(@"Video",nil),[assetItems count]+1];
  27.  
  28. [self performSelector:@selector(imageFromVideoURL)];
  29. [dic setValue:title forKey:kName];
  30. [dic setValue:appDelegate.videoURL forKey:kURL];
  31.  
  32. AssetBrowserItem *item = [[AssetBrowserItem alloc] initWithURL:appDelegate.videoURL title:title];
  33. [assetItems addObject:item];
  34. [appDelegate.videoURLArray addObject:dic];
  35.  
  36. NSLog(@"Video has info:%@",appDelegate.videoURLArray);
  37. }
  38. NSLog(@"Values of dictonary==>%@",dic);
  39.  
  40. //NSLog(@"assetItems:%@",assetItems);
  41. NSLog(@"Videos Are:%@",appDelegate.videoURLArray);
  42. } ];
  43. }
  44. // group == nil signals we are done iterating.
  45. else
  46. {
  47. dispatch_async(dispatch_get_main_queue(),^{
  48. //[self updateBrowserItemsAndSignalDelegate:assetItems];
  49. loadImgView.hidden = NO;
  50. [spinner stopAnimating];
  51. [loadImgView removeFromSuperview];
  52. //selectVideoBtn .userInteractionEnabled = YES;
  53. });
  54. }
  55. }
  56. failureBlock:^(NSError *error)
  57. {
  58. NSLog(@"error enumerating AssetLibrary groups %@\n",error);
  59. }];
  60. }
  61.  
  62. - (UIImage *)imageFromVideoURL
  63. {
  64. // result
  65. UIImage *image = nil;
  66.  
  67. // AVAssetImageGenerator
  68. AVAsset *asset = [[AVURLAsset alloc] initWithURL:appDelegate.videoURL options:nil];;
  69. AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
  70. imageGenerator.appliesPreferredTrackTransform = YES;
  71.  
  72. // calc midpoint time of video
  73. Float64 durationSeconds = CMTimeGetSeconds([asset duration]);
  74. CMTime midpoint = CMTimeMakeWithSeconds(durationSeconds/2.0,600);
  75.  
  76. // get the image from
  77. NSError *error = nil;
  78. CMTime actualTime;
  79. CGImageRef halfWayImage = [imageGenerator copyCGImageAtTime:midpoint actualTime:&actualTime error:&error];
  80.  
  81. if (halfWayImage != NULL)
  82. {
  83. // cgimage to uiimage
  84. image = [[UIImage alloc] initWithCGImage:halfWayImage];
  85. [dic setValue:image forKey:kImage];
  86. NSLog(@"Values of dictonary==>%@",dic);
  87. NSLog(@"Videos Are:%@",appDelegate.videoURLArray);
  88. CGImageRelease(halfWayImage);
  89. }
  90. return image;
  91. }
  92.  
  93. - (void)assetsLibraryDidChange:(NSNotification*)changeNotification
  94. {
  95. [self updateAssetsLibrary];
  96. }
  97.  
  98. - (void)buildAssetsLibrary
  99. {
  100. assetsLibrary = [[ALAssetsLibrary alloc] init];
  101. ALAssetsLibrary *notificationSender = nil;
  102.  
  103. NSString *minimumSystemVersion = @"4.1";
  104. NSString *systemVersion = [[UIDevice currentDevice] systemVersion];
  105. if ([systemVersion compare:minimumSystemVersion options:NSNumericSearch] != NSOrderedAscending)
  106. notificationSender = assetsLibrary;
  107.  
  108. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(assetsLibraryDidChange:) name:ALAssetsLibraryChangedNotification object:notificationSender];
  109. [self updateAssetsLibrary];
  110. }

代码将为您提供iPhone的视频列表.

它可能会帮助你感谢:)

猜你在找的iOS相关文章