iPhone:获取音频文件的持续时间 [英] iPhone: get duration of an audio file

查看:591
本文介绍了iPhone:获取音频文件的持续时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

获得音频文件持续时间的最简单方法是什么?

What is the easiest way to get a duration of an audio file?

我可以创建一个AVAudioPlayer对象,用URL初始化,但这种方式太长了。有更简单的方法吗?

I could create an object of AVAudioPlayer, initialize it with URL and than get the duration, but this way is too long. Is there an easier way?

谢谢。

推荐答案

请使用音频文件服务功能。有一个属性,应该给你的估计持续时间。代码:

You can use the Audio File Services functions. There's one property to get that should give you the estimated duration. Code:

    NSURL *afUrl = [NSURL fileURLWithPath:soundPath];
    AudioFileID fileID;
    OSStatus result = AudioFileOpenURL((CFURLRef)afUrl, kAudioFileReadPermission, 0, &fileID);
    UInt64 outDataSize = 0;
    UInt32 thePropSize = sizeof(UInt64);
    result = AudioFileGetProperty(fileID, kAudioFilePropertyEstimatedDuration, &thePropSize, &outDataSize);
    AudioFileClose(fileID);

您可以检查文档此处

这篇关于iPhone:获取音频文件的持续时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆