【问题标题】:Getting the time property of an audio file in java在java中获取音频文件的时间属性
【发布时间】:2014-12-04 16:13:03
【问题描述】:

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

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

    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();

【问题讨论】:

    标签: java audio


    【解决方案1】:

    这是一个处理我使用的 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
    }
    

    【讨论】:

    • 这可能,谢谢!现在要玩它了。让你知道它是否成功了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-01
    • 2014-08-25
    • 2014-02-08
    • 2010-11-17
    • 2020-06-02
    • 1970-01-01
    • 2015-10-13
    相关资源
    最近更新 更多