// 取得音频样本 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; }
private void soundAbspielen(File sound) { if (!läuft) return; try { audioInputStream = AudioSystem.getAudioInputStream(sound); af = audioInputStream.getFormat(); size = (int) (af.getFrameSize() * audioInputStream.getFrameLength()); audio = new byte[size]; info = new DataLine.Info(Clip.class, af, size); audioInputStream.read(audio, 0, size); clip = (Clip) AudioSystem.getLine(info); clip.open(af, audio, 0, size); clip.start(); } catch (Exception e) { e.printStackTrace(); } }
public boolean load(File file) { this.file = file; if (file != null && file.isFile()) { try { errStr = null; audioInputStream = AudioSystem.getAudioInputStream(file); fileName = file.getName(); format = audioInputStream.getFormat(); } catch (Exception ex) { reportStatus(ex.toString()); return false; } } else { reportStatus("Audio file required."); return false; } numChannels = format.getChannels(); sampleRate = (double) format.getSampleRate(); sampleBitSize = format.getSampleSizeInBits(); long frameLength = audioInputStream.getFrameLength(); long milliseconds = (long) ((frameLength * 1000) / audioInputStream.getFormat().getFrameRate()); double audioFileDuration = milliseconds / 1000.0; if (audioFileDuration > MAX_AUDIO_DURATION) duration = MAX_AUDIO_DURATION; else duration = audioFileDuration; frameLength = (int) Math.floor((duration / audioFileDuration) * (double) frameLength); try { audioBytes = new byte[(int) frameLength * format.getFrameSize()]; audioInputStream.read(audioBytes); } catch (Exception ex) { reportStatus(ex.toString()); return false; } getAudioData(); return true; }
/** 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); }