2014-12-04 29 views
0

讀取音頻文件(例如wav文件),然後獲取其時間長度。然後從第二個秒或第二個秒獲取它的字節值。就像我有一個20秒的WAV,我會在我指定的時間輸出它的字節[]。因爲獲取文件的所有長度的字節需要非常大的空間。在java中獲取音頻文件的時間屬性

這是我從音頻文件中獲取字節,但我需要的只是秒的字節。任何幫助?

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

回答

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 
} 
+0

這可能,謝謝!現在就玩吧。讓你知道它是否有竅門。 – dimmed 2014-12-04 17:43:40

相關問題