如何在iOS音频文件的时间? [英] How to get the duration of an audio file in iOS?

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

问题描述

NSDictionary* fileAttributes = 
    [[NSFileManager defaultManager] attributesOfItemAtPath:filename 
                                                     error:nil]

从文件属性键,你可以得到的日期,大小等,但你怎么弄的期限?

From the file attribute keys, you can get the date, size, etc. But how do you get the duration?

推荐答案

在<一个href=\"http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/Reference/Reference.html\">'File属性键的的的NSFileManager 的类引用你可以看到,没有钥匙使用,将返回一首歌的时间。的所有信息,该的NSFileManager 的实例获取有关一个文件是与操作系统内的实际文件本身的性能,例如它的文件大小做。在的NSFileManager 的实际上不跨preT文件。

In the 'File Attribute Keys' of the NSFileManager class reference you can see that there is no key to use that will return the duration of a song. All the information that the NSFileManager instance gets about a file is to do with the properties of the actual file itself within the operating system, such as its file-size. The NSFileManager doesn't actually interpret the file.

为了获取文件的时间,你需要使用一个知道如何跨preT文件的类。在 AVFoundation 的框架提供你所需要的确切类,的 AVAsset 的。您可以使用具体子类的 AVURLAsset 的实例化这个抽象类的一个实例,然后提供一个它的 NSURL 的指向你希望得到持续的音频文件。然后,您可以从持续时间的 AVAsset 的实例,通过查询其持续时间属性。

In order to get the duration of the file, you need to use a class that knows how to interpret the file. The AVFoundation framework provides the exact class you need, AVAsset. You can instantiate an instance of this abstract class using the concrete subclass AVURLAsset, and then provide it an NSURL which points to the audio file you wish to get the duration. You can then get the duration from the AVAsset instance by querying its duration property.

例如:

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

请注意该AVFoundation被设计为一个重异步框架,以提高性能和总体用户体验。即使执行简单的任务,如查询的媒体文件的持续时间可能会采取很长一段时间,并可能导致应用程序挂起。你应该使用AVAsynchronousKeyValueLoading协议的异步加载歌曲的持续时间,然后更新完成处理程序块的UI。你应该看一看的<一个href=\"http://developer.apple.com/library/ios/#featuredarticles/Short_Practical_Guide_Blocks/_index.html\">'Block编程指南还有WWDC2010视频名为情迷AV基金会',这是免费提供的https://developer.apple.com/videos/wwdc/2010.

Note that AVFoundation is designed as a heavily asynchronous framework in order to improve performance and the overall user experience. Even performing simple tasks such as querying a media file's duration can potentially take a long period of time and can cause your application to hang. You should use the AVAsynchronousKeyValueLoading protocol to asynchronously load the duration of the song, and then update your UI in a completion handler block. You should take a look at the 'Block Programming Guide' as well as the WWDC2010 video titled, 'Discovering AV Foundation', which is available free at https://developer.apple.com/videos/wwdc/2010.

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

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