【问题标题】:Get audio file size without export无需导出即可获取音频文件大小
【发布时间】:2013-12-19 14:54:14
【问题描述】:

我正在开发一个导出存储在 iPod 库中的音频文件的应用程序,但我应该在导出和上传之前验证文件大小(服务器具有固定的最大上传大小):

  • 我知道导出文件后大小会不一样:有什么方法可以估算出新的大小吗?
  • 主要问题:我可以在导出之前知道原始文件的大小(使用 MPMediaItem 或其他东西),所以我可以告诉用户这个文件无法上传(导出可能需要一段时间)。 谢谢。

【问题讨论】:

  • 您在 ios 6 或 5 中的部署目标
  • 我也有同样的问题。在我的情况下,estimatedOutputFileLength 返回 0。有什么想法吗?

标签: ios iphone mpmediaitem mpmediapickercontroller


【解决方案1】:

我没有测试过,但是 AVAssetExportSession 可以帮助你。

MPMediaItem *curItem = musicPlayer.nowPlayingItem;

NSURL *url = [curItem valueForProperty: MPMediaItemPropertyAssetURL];

AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL: url options:nil];

AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset: songAsset
                                   presetName: AVAssetExportPresetPassthrough];

exporter.estimatedOutputFileLength 将为您提供以字节为单位的大小。

【讨论】:

  • 是的,谢谢 :),但是我们需要向导出器添加 timeRange : AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset: songAsset presetName: AVAssetExportPresetAppleM4A]; CMTime half = CMTimeMultiplyByFloat64(exporter.asset.duration, 1); exporter.timeRange = CMTimeRangeMake(kCMTimeZero, half); long long size = exporter.estimatedOutputFileLength;
  • 不能给你+50!链接好像失效了,怎么办?
  • 你问的是哪个链接?
  • 您无法使用 AVAssetExportPresetPassthrough 获得估计的OutputFileLength,如果使用不同的预设,您还需要 timeRange
  • 我也有同样的问题。在我的情况下,estimatedOutputFileLength 返回 0。有任何想法吗 ?我正在使用 swift 4
【解决方案2】:

我不确定,但我认为你可以使用:

MPMediaItem *item = [[collection items] objectAtIndex:0];
NSURL *url = [item valueForProperty:MPMediaItemPropertyAssetURL];
NSData *data = [NSData dataWithContentsOfURL:url];
NSLog(@"length %d",[data length]);

问候,

马里亚诺

【讨论】:

  • iOS 6,有区别吗?
猜你喜欢
  • 2011-01-01
  • 1970-01-01
  • 2012-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-19
相关资源
最近更新 更多