复制wav文件

时间:2014-05-17 23:22:27

标签: java file audio copy wav

我按照here

的说明复制了一个wav文件

尝试播放我的新文件时,它会运行,但听不到声音。有人可以帮忙吗? 我的阅读和写作课程在网站的另一篇文章中有解释:

这是读取文件的函数:

// read a wav file into this class
public boolean read(DataInputStream inFile) {
    myData = null;
    byte[] tmpInt = new byte[4];
    byte[] tmpShort = new byte[2];
//        byte[] buffer= new byte[offset];
    try {
        System.out.print(inFile.available());
    } catch (IOException ex) {
        Logger.getLogger(Wav.class.getName()).log(Level.SEVERE, null, ex);
    }
    try {
       // if (offset!=0)
        //    inFile.read(buffer, 0,offset);

        //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(tmpInt); // read the ChunkSize
        myChunkSize = byteArrayToIntLittle(tmpInt);

        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(tmpInt); // read the SubChunk1Size
        mySubChunk1Size = byteArrayToIntLittle(tmpInt);

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

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

        inFile.read(tmpInt); // read the samplerate
        mySampleRate = byteArrayToIntLittle(tmpInt);

        inFile.read(tmpInt); // read the byterate
        myByteRate = byteArrayToIntLittle(tmpInt);

        inFile.read(tmpShort); // read the blockalign
        myBlockAlign = byteArrayToShortLittle(tmpShort);

        inFile.read(tmpShort); // read the bitspersample
        myBitsPerSample = byteArrayToShortLittle(tmpShort);

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

        inFile.read(tmpShort);
        extraParams= byteArrayToShortLittle(tmpShort);

        // 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(tmpInt); // read the size of the data
        myDataSize = byteArrayToIntLittle(tmpInt);
        // read the data chunk
        myData = new byte[(int) myDataSize];

        // close the input stream
        inFile.close();

    } catch (Exception e) {
       System.out.print(e);
        return false;
    }

    return true; 
}

然后将其保存为新的wav文件我使用此功能:

  public boolean save() {
    try {
        // Creating the  file
           File file = new File(myPath);
           boolean isCreated = false;
           try {
                   isCreated = file.createNewFile();
                   if (!isCreated) {
                           file.delete();
                           file.createNewFile();
                   }
           } catch (IOException e) {
                   e.printStackTrace();
                   return false;
           }

        DataOutputStream outFile = new DataOutputStream(new FileOutputStream(myPath));

        // write the wav file per the wav file format
        outFile.writeBytes("RIFF");                 // 00 - RIFF
        outFile.write(intToByteArray((int) myChunkSize), 0, 4);     // 04 - how big is the rest of this file?
        outFile.writeBytes("WAVE");                 // 08 - WAVE
        outFile.writeBytes("fmt ");                 // 12 - fmt
        outFile.write(intToByteArray((int) mySubChunk1Size), 0, 4); // 16 - size of this chunk
        outFile.write(shortToByteArray((short) myFormat), 0, 2);        // 20 - what is the audio format? 1 for PCM = Pulse Code Modulation
        outFile.write(shortToByteArray((short) myChannels), 0, 2);  // 22 - mono or stereo? 1 or 2?  (or 5 or ???)
        outFile.write(intToByteArray((int) mySampleRate), 0, 4);        // 24 - samples per second (numbers per second)
        outFile.write(intToByteArray((int) myByteRate), 0, 4);      // 28 - bytes per second
        outFile.write(shortToByteArray((short) myBlockAlign), 0, 2);    // 32 - # of bytes in one sample, for all channels
        outFile.write(shortToByteArray((short) myBitsPerSample), 0, 2); // 34 - how many bits in a sample(number)?  usually 16 or 24
        outFile.write(shortToByteArray((short) extraParams), 0, 2);
        outFile.writeBytes("data");                 // 36 - data
        outFile.write(intToByteArray((int) myDataSize), 0, 4);      // 40 - how big is this data chunk
        outFile.write(myData);                      // 44 - the actual data itself - just a long string of numbers

        System.out.print("size of wav "+ outFile.size()+ "\n");
        outFile.close();
    } catch (Exception e) {
        System.out.println(e);
        return false;
    }

    return true;
}

1 个答案:

答案 0 :(得分:1)

你忽略了每个read()返回的计数。它没有义务传输多个字节。您应该使用DataInputStream.readFully()来确保缓冲区都被填充。

但是,除非您正在修改未说明的数据,否则不需要任何此类数据。只需使用标准的五行复制循环复制字节即可。

NB你不需要所有的exists()/ isCreated()/ delete()东西。 'new FileOutputStream()'已经完成了所有这些。