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

删除回忆录丶 提交于 2019-12-04 07:55:35

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]);
Nazir

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);

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

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