package boondock.holdem.Sound;
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
public class waveObj {
private static final int EXTERNAL_BUFFER_SIZE = 128000;
SourceDataLine line = null;
private waveObjPlayer player;
boolean wpausehit = false;
private long position;
private String file;
/**
* Creates a new wave object.
*/
public waveObj() { position = 0; }
/**
* Creates a new wave object from a file name.
*
* @param file the file name of the WAVE song
*/
public waveObj(String file) { setFile(file); }
/**
* Gets the file name of the wave file.
*
* @return the file name of the wave file.
*/
public String getFile() { return file; }
/**
* Sets the file name of the wave file.
*
* @param file the file name of the wave file.
*/
private void setFile(String file) { this.file = file; }
/**
* Converts the song to a string. Basically, just gives the file name.
*
* @return the string representation of the song
*/
public String toString() {
File f = new File(getFile());
return f.getName();
}
/**
* Checks if a named file is a valid wave file.
*
* @param file the file name to be ckecked
* @return true
if the file is a WAVE, false
* otherwise throw exception
*/
public static boolean isWave(String file) {
try {
AudioSystem.getAudioFileFormat(new File(file));
}
catch (Exception exception) {
return false;
}
return true;
}
//public int getLength() { return 0; }
/**
* Begins playback of the wave file object.
*
*/
private void play() {
wpausehit = false;
player = new waveObjPlayer();
player.start();
}
/**
* Take in the name of the wave file object and begins playback of the wave file object.
*
*/
public void play(String file) {
this.file = file;
wpausehit = false;
player = new waveObjPlayer();
player.start();
}
private static void out(String strMessage) {
System.out.println(strMessage);
}
/**
* Pauses playback of wave file object.
*/
public void pause() {
if(wpausehit) {
wpausehit = false;
player.unpause();
}
else {
wpausehit = true;
player.pause();
}
}
/**
* Terminates playback of wave file object.
*/
public void stop() {
player.end();
//might crash if song is not currently playing!
}
private class waveObjPlayer extends Thread {
private boolean playing;
public waveObjPlayer() { playing = false; }
public void run() {
playing = true;
File soundFile = new File(file);
/*
Read in the sound file through audio input stream.
*/
AudioInputStream audioInputStream = null;
try {
audioInputStream = AudioSystem.getAudioInputStream(soundFile);
}
catch (Exception exception) {
//System.out.println("You do not have a sound card, and therefore CANNOT play sound on your computer.");
//System.exit(1);
System.out.println("Sound exception error.");
}
AudioFormat audioFormat = audioInputStream.getFormat();
line = null;
DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
try {
line = (SourceDataLine) AudioSystem.getLine(info); //get a line
line.open(audioFormat); //open it for audio data
}
catch (Exception exception) {
//System.out.println("You do not have a sound card, and therefore CANNOT play sound on your computer.");
//System.exit(1);
System.out.println("Sound exception error.");
}
line.start();
int nBytesRead = 0;
byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];
/*
Loop playback
*/
while (nBytesRead != -1 && playing) {
try {
nBytesRead = audioInputStream.read(abData, 0, abData.length);
}
catch (IOException exception) {
//System.out.println("You do not have a sound card, and therefore CANNOT play sound on your computer.");
//System.exit(1);
System.out.println("Sound exception error.");
}
if (nBytesRead >= 0) {
int nBytesWritten = line.write(abData, 0, nBytesRead);
}
}
line.drain();
line.close();
//
}
public void end() {
playing = false;
}
public void pause() {
line.stop();
}
public void unpause() {
line.start();
}
}
}