问题描述
我正在制作节奏游戏,我需要一种快速的方法来获取ogg文件的长度.我能想到的唯一方法是在不播放文件的情况下真正快速地传输文件,但是如果我有数百首歌曲,这显然是不现实的.另一种方法是将文件的长度存储在某种属性文件中,但我想避免这种情况.我知道一定有某种方法可以做到这一点,因为大多数音乐播放器可以告诉您一首歌曲的长度.
I'm making a rhythm game and I need a quick way to get the length of an ogg file. The only way I could think would be to stream the file really fast without playing it but if I have hundreds of songs this would obviously not be practical. Another way would be to store the length of the file in some sort of properties file but I would like to avoid this. I know there must be some way to do this as most music players can tell you the length of a song.
推荐答案
最快的方法是查找文件的末尾,然后返回找到的最后一个Ogg页面标题并读取其granulePosition(是文件中每个通道的样本总数).这并不是万无一失的(您可能正在查看一个链接的文件,在这种情况下,您只能得到最后一个流的长度),但是应该可以用于绝大多数Ogg文件.
The quickest way to do it is to seek to the end of the file, then back up to the last Ogg page header you find and read its granulePosition (which is the total number of samples per channel in the file). That's not foolproof (you might be looking at a chained file, in which case you're only getting the last stream's length), but should work for the vast majority of Ogg files out there.
如果在读取Ogg页面标题时需要帮助,则可以阅读Jorbis源代码...简短版本是查找"OggS",读取一个字节(应为0),读取一个字节(仅一位)应该设置为3),然后读取一个64位Little Endian值.
If you need help with reading the Ogg page header, you can read the Jorbis source code... The short version is to look for "OggS", read a byte (should be 0), read a byte (only bit 3 should be set), then read a 64-bit little endian value.
这篇关于如何确定OGG文件的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!