【问题标题】:How to find an audio file's length (in seconds)如何查找音频文件的长度(以秒为单位)
【发布时间】:2011-06-05 12:51:45
【问题描述】:

(目标 C) 仅使用简单的 AudioServicesPlaySystemSoundID 及其对应项,但如果已经有办法找到音频文件的长度,我无法在文档中找到。

我知道有 AudioServicesGetPropertyInfo,但这似乎返回了一个字节缓冲区 - 音频文件是否将它们的长度嵌入到自身中,我可以用它来提取它吗?

或者是否有一个基于比特率*文件大小的公式来转换为时间长度?

mIL3S

www.milkdrinkingcow.com

【问题讨论】:

    标签: iphone objective-c audio core-audio


    【解决方案1】:

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

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

    【讨论】:

    • 是的,我想我应该只使用 AVAudioPlayer。我想因为我只是在播放一些小声音,所以 AudioServices 还可以;现在我需要更多信息,我可能应该切换。谢谢你的公式!我虽然可能有类似的东西。 mIL3S
    【解决方案2】:

    来自答案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);
    

    【讨论】:

    • 这是实现此 IMO 的最佳通用方法。
    【解决方案3】:

    参见:

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

    请参阅 ADC 示例: http://developer.apple.com/library/mac/#samplecode/PlayFile/Introduction/Intro.html

    不使用 AVPlayer...

    【讨论】:

    • 但这不只适用于 Mac OS X 吗?该问题用 iPhone 标记。
    猜你喜欢
    • 1970-01-01
    • 2017-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多