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

2019-04-08 19:45发布

(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

3条回答
别忘想泡老子
2楼-- · 2019-04-08 20:09

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...

查看更多
The star\"
3楼-- · 2019-04-08 20:12

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]);
查看更多
再贱就再见
4楼-- · 2019-04-08 20:22

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);
查看更多
登录 后发表回答