How to find an audio file's length (in seconds)

余生颓废 提交于 2019-12-06 02:35:10

问题


(Objective C) Just using simple AudioServicesPlaySystemSoundID and its counterparts, but I can't find in the documentation if there is already a way to find the length of an audio file.

I know there is AudioServicesGetPropertyInfo, but that seems to return a byte-buffer - do audio files embed their length in themselves and I can just extract it with this?

Or is there perhaps a formula based on bit-rate * fileSize to convert to length-of-time?

mIL3S

www.milkdrinkingcow.com


回答1:


According to a quick Google search, there is a formula:

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

Is there any particular reason you're using System Sound Services to play a sound?

If you use AVAudioPlayer to handle your sounds you could do something like:

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



回答2:


sample code from answer How to get the duration of an audio file in iOS?. This is the best answer.

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



回答3:


See at:

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

see ADC sample at: http://developer.apple.com/library/mac/#samplecode/PlayFile/Introduction/Intro.html

without using AVPlayer...



来源:https://stackoverflow.com/questions/4630176/how-to-find-an-audio-files-length-in-seconds

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!