如何找到MP3音频剪辑的标题?

问题描述 投票:0回答:1

我试图解决的主要问题是找到以字节数组形式给出的MP3音频片段的长度。为了找到长度,我需要在剪辑中找到frames的数量。根据MP3 spec,帧是一个字节,其中11个连续位设置为1;即一个值为255的字节,后跟一个字节,其中3的最高有效位设置为1(十进制值224)。基本上,一旦我找到255的字节值,我正在检查下一个字节的值是否为224.但是根据这个标准,我无法在数组中找到任何帧头。另外,引用文档还提到255之后的下一个字节有时可能将其4个MSB设置为1.这样我应该在255之后搜索值240.无论是哪种情况,我都无法找到任何帧头。请告知我这样做是否正确。

这是我的代码:

public class AudioUtils
{  
    public static int getAudioLength(byte[] audio, AudioFormat audioFormat, int samples, int sampleRate)
    {
        if (!audioFormat.getId().equals(AudioFormat.MP3))
            throw new UnsupportedOperationException(String.format("Audio length computation for audio of format %s is not supported", audioFormat.getFormat()));

        int frames = getNumberOfFrames(audio);
        return ((frames * samples) / sampleRate);
    }

    private static int getNumberOfFrames(byte[] audio)
    {
        int frames = 0;
        for (int i = 0; i < audio.length; i++)
        {
            byte b = audio[i];
            int byteInt = b & 0xff; // convert byte to int
            if (byteInt == 255)
            {
                // If there is another byte in the array
                if (i + 1 < audio.length && (audio[i + 1] & 0xff) == 224)
                {
                    // We have found an header. Increment the frames count.
                    frames++;
                }
            }
        }

        return frames;
    }
}
java arrays audio byte mp3
1个回答
1
投票

由于同步字只有11位且您不知道剩余5位的内容,因此您只需要比较第二个字节的前3位。实现此目的的常用方法是使用二进制“&”运算符将要忽略的位设置为0(称为屏蔽),并在要尝试比较的常量中将这些位设置为0。

在下面的示例中,第二个字节用0xe0屏蔽,并与相同的值进行比较。

for (int i = 0; i < audio.length - 1; i++)
{
    if (audio[i] == 0xff && (audio[i+1] & 0xe0) == 0xe0)
    {
        frames++;
    }
} 
© www.soinside.com 2019 - 2024. All rights reserved.