Created
November 16, 2012 17:53
-
-
Save Tiim/4089418 to your computer and use it in GitHub Desktop.
Java Play Sound
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.wikijava.sound.playWave; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import javax.sound.sampled.AudioFormat; | |
import javax.sound.sampled.AudioInputStream; | |
import javax.sound.sampled.AudioSystem; | |
import javax.sound.sampled.LineUnavailableException; | |
import javax.sound.sampled.SourceDataLine; | |
import javax.sound.sampled.UnsupportedAudioFileException; | |
import javax.sound.sampled.DataLine.Info; | |
public class PlaySound { | |
private InputStream waveStream; | |
private final int EXTERNAL_BUFFER_SIZE = 524288; // 128Kb | |
public PlaySound(InputStream waveStream) { | |
this.waveStream = waveStream; | |
} | |
public void play() { | |
AudioInputStream audioInputStream = null; | |
try { | |
audioInputStream = AudioSystem.getAudioInputStream(this.waveStream); | |
} catch (UnsupportedAudioFileException e1) { | |
e1.printStackTrace(); | |
} catch (IOException e1) { | |
e1.printStackTrace(); | |
} | |
// Obtain the information about the AudioInputStream | |
AudioFormat audioFormat = audioInputStream.getFormat(); | |
Info info = new Info(SourceDataLine.class, audioFormat); | |
// opens the audio channel | |
SourceDataLine dataLine = null; | |
try { | |
dataLine = (SourceDataLine) AudioSystem.getLine(info); | |
dataLine.open(audioFormat, this.EXTERNAL_BUFFER_SIZE); | |
} catch (LineUnavailableException e1) { | |
e1.printStackTrace(); | |
} | |
// Starts the music :P | |
dataLine.start(); | |
int readBytes = 0; | |
byte[] audioBuffer = new byte[this.EXTERNAL_BUFFER_SIZE]; | |
try { | |
while (readBytes != -1) { | |
readBytes = audioInputStream.read(audioBuffer, 0, | |
audioBuffer.length); | |
if (readBytes >= 0){ | |
dataLine.write(audioBuffer, 0, readBytes); | |
} | |
} | |
} catch (IOException e1) { | |
e1.printStackTrace(); | |
} finally { | |
// plays what's left and and closes the audioChannel | |
dataLine.drain(); | |
dataLine.close(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment