2011-05-13 85 views
2

假设我从iPhone库中取得了一个视频文件。我想要检查视频文件不应该大于2MB。如何检查视频文件是否大于2MB?

我不能使用videoMaximumDuration方法。因为如果任何视频是高质量的,即使1分钟的视频可能会很大。

任何视图?

回答

3

urlvideo包含所选视频文件的URL

  NSString *strurl=[urlvideo path]; 
      NSFileManager *fileManager = [NSFileManager defaultManager]; 
      NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:strurl error:nil]; 

     if(fileAttributes != nil) 
      { 
       NSString *fileSize = [fileAttributes objectForKey:NSFileSize]; 
       //NSLog(@"File size: %@ kb", fileSize);    
       if ([fileSize intValue] > 2000000) {      
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"File size greater than 2MB.Please select another video file." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
        [alert show]; 
        [alert release]; 
       }    
       else { 
NSLog(@"video size less than 2 mb"); 
    } 
0
0

NSURL解决方案将无法工作,因为用户的iPod或照片库中的视频网址不会是文件网址,而是MediaPlayer或ALAssetLibrary处理的特殊方案。 (我对ALAssetLibrary这样做并不积极,但我知道MediaPlayer会这样做,并且会想象照片库也会这样做,所以你不能在背后隐藏东西)。

我能想到的最佳解决方案是使用URL创建一个AVURLAsset,然后迭代遍历轨道并将估计的数据速率乘以轨道持续时间(以秒为单位)。这应该会给你一个粗略的估计每个轨道。