【问题标题】:Copying wav files复制 wav 文件
【发布时间】:2014-07-06 04:37:55
【问题描述】:

我已经按照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;
}

【问题讨论】:

标签: java file audio copy wav


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-28
    • 1970-01-01
    • 1970-01-01
    • 2012-05-26
    • 2013-03-24
    • 1970-01-01
    • 2014-04-17
    • 2014-07-27
    相关资源
    最近更新 更多