在java中获取音频文件的time属性

时间:2014-12-04 16:13:04

标签: java audio

读取音频文件,例如wav文件,然后获取其时间长度。然后从秒到秒或半秒获取其字节值。就像我有一个20秒的wav,我将在我指定的时间输出其字节[]。因为获取文件所有长度的字节占用非常大的空间。

这是我从音频文件中获取字节,但我只需要按秒的字节数。有什么帮助吗?

    FileInputStream s = new FileInputStream(Environment.getExternalStorageDirectory().getAbsolutePath()+"/audio_raw.wav");
    BufferedInputStream b = new BufferedInputStream(s);
    byte[] data = new byte[128];

    while((bytes = b.read(data)) > 0)
    {
        for(int i = 0; i<bytes; i++)
        {
            unsigned = data[i] & 0xFF;
            bw.write(unsigned+"+");
        }
    }

    b.read(data);
    b.close();

1 个答案:

答案 0 :(得分:0)

这是一个处理我使用的WAV文件的类的函数。您可以看到它在文件中的读取方式,并可以提取有关WAV文件的一些数据。也许它可以帮助你:

// read a wav file into this class
public boolean read()
{
    DataInputStream inFile = null;
    myData = null;
    byte[] tmpLong = new byte[4];
    byte[] tmpInt = new byte[2];

    try
    {
        inFile = new DataInputStream(new FileInputStream(myPath));

        //System.out.println("Reading wav file...\n"); // for debugging only

        String chunkID = "" + (char) inFile.readByte() + (char) inFile.readByte() + (char) inFile.readByte() + (char) inFile.readByte();

        inFile.read(tmpLong); // read the ChunkSize
        myChunkSize = byteArrayToLong(tmpLong);

        String format = "" + (char) inFile.readByte() + (char) inFile.readByte() + (char) inFile.readByte() + (char) inFile.readByte();

        // print what we've read so far
        //System.out.println("chunkID:" + chunkID + " chunk1Size:" + myChunkSize + " format:" + format); // for debugging only



        String subChunk1ID = "" + (char) inFile.readByte() + (char) inFile.readByte() + (char) inFile.readByte() + (char) inFile.readByte();

        inFile.read(tmpLong); // read the SubChunk1Size
        mySubChunk1Size = byteArrayToLong(tmpLong);

        inFile.read(tmpInt); // read the audio format.  This should be 1 for PCM
        myFormat = byteArrayToInt(tmpInt);

        inFile.read(tmpInt); // read the # of channels (1 or 2)
        myChannels = byteArrayToInt(tmpInt);

        inFile.read(tmpLong); // read the samplerate
        mySampleRate = byteArrayToLong(tmpLong);

        inFile.read(tmpLong); // read the byterate
        myByteRate = byteArrayToLong(tmpLong);

        inFile.read(tmpInt); // read the blockalign
        myBlockAlign = byteArrayToInt(tmpInt);

        inFile.read(tmpInt); // read the bitspersample
        myBitsPerSample = byteArrayToInt(tmpInt);

        // print what we've read so far
        //System.out.println("SubChunk1ID:" + subChunk1ID + " SubChunk1Size:" + mySubChunk1Size + " AudioFormat:" + myFormat + " Channels:" + myChannels + " SampleRate:" + mySampleRate);


        // read the data chunk header - reading this IS necessary, because not all wav files will have the data chunk here - for now, we're just assuming that the data chunk is here
        String dataChunkID = "" + (char) inFile.readByte() + (char) inFile.readByte() + (char) inFile.readByte() + (char) inFile.readByte();

        inFile.read(tmpLong); // read the size of the data
        myDataSize = byteArrayToLong(tmpLong);


        // read the data chunk
        myData = new byte[(int) myDataSize];
        inFile.read(myData);

        // close the input stream
        inFile.close();
    }
    catch (Exception e)
    {
        return false;
    }

    return true; // this should probably be something more descriptive
}