// 取得音频样本 private byte[] getAudioSamples(AudioInputStream MusicStream, AudioFormat format) { int AudioSampleLengh = (int) (MusicStream.getFrameLength() * format.getFrameSize()); byte aAudioSamples[] = new byte[AudioSampleLengh]; DataInputStream dataInputStream = new DataInputStream(MusicStream); try { dataInputStream.readFully(aAudioSamples); } catch (Exception e) { e.printStackTrace(); } return aAudioSamples; }
/** Loads a Sound from an AudioInputStream. */ public Sound getSound(AudioInputStream audioStream) { if (audioStream == null) { return null; } // get the number of bytes to read int length = (int) (audioStream.getFrameLength() * audioStream.getFormat().getFrameSize()); // read the entire stream byte[] samples = new byte[length]; DataInputStream is = new DataInputStream(audioStream); try { is.readFully(samples); is.close(); } catch (IOException ex) { ex.printStackTrace(); } // return the samples return new Sound(samples); }