如何查找音频文件的长度(以秒为单位)

问题描述:

(Objective C) 只使用简单的AudioServicesPlaySystemSoundID及其对应物,但在文档中找不到已存在的方法来查找音频文件的长度。如何查找音频文件的长度(以秒为单位)

我知道有AudioServicesGetPropertyInfo,但似乎返回一个字节缓冲区 - 做音频文件嵌入自己的长度,我可以提取它与此?

或者有可能是一个公式基于比特率* fileSize转换为长度的时间?

mIL3S

www.milkdrinkingcow.com

根据快速谷歌搜索,有一个公式:

length-of-time (duration in seconds) = fileSize (in bytes)/bit-rate (bits/secs)*8 

有你使用系统声音服务于任何特别的理由播放声音?

如果使用AVAudioPlayer来处理你的声音,你可以这样做:

AVAudioPlayer * sound = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:nil]; 
sound.delegate = self; 
sound.volume = 1; 
NSLog([NSString stringWithFormat:@"%f", sound.duration]); 
+0

雅,我想我应该只使用AVAudioPlayer。我想因为我只是在播放一些小声音,所以AudioServices还好;现在我需要更多的信息,我应该切换。尽管感谢您的公式!我虽然可能有这样的事情。 mIL3S – 2011-01-07 23:04:43

看到:

Float64 fileDuration = PrepareFileAU (fileAU, fileFormat, audioFile); 

看到ADC采样: http://developer.apple.com/library/mac/#samplecode/PlayFile/Introduction/Intro.html

不使用AVPlayer。 ..

+0

但是,这不仅适用于Mac OS X?问题用iPhone标记。 – newenglander 2013-07-03 13:40:08

来自a的示例代码请拨打How to get the duration of an audio file in iOS?。这是最好的答案。

AVURLAsset* audioAsset = [AVURLAsset URLAssetWithURL:audioFileURL options:nil]; CMTime audioDuration = audioAsset.duration; float audioDurationSeconds = CMTimeGetSeconds(audioDuration);

+0

这是实现这个IMO的最佳通用方法。 – theLastNightTrain 2014-06-23 15:13:51