//try it
import javax.sound.sampled.AudioInputStream;
import
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.TargetDataLine;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.AudioSystem;
import
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
public class Playback extends Thread //implements Runnable
{
final int bufSize = 16384;
SourceDataLine line;
File file;
String fileName = "untitled";
AudioInputStream audioInputStream;
long duration;
public Playback(String strFilename)
{
file = new File(strFilename);
setName("Playback");
}
private void shutDown(String message)
{
if (message != null)
{
System.err.println(message);
}
}
public void run()
{
// reload the file if loaded by file
if (file != null)
{
createAudioInputStream(file);
}
// make sure we have something to play
if (audioInputStream == null)
{
shutDown("No loaded audio to play back");
return;
}
// get an AudioInputStream of the desired format for playback
AudioFormat format = audioInputStream.getFormat();
AudioInputStream playbackInputStream = AudioSystem.getAudioInputStream(format, audioInputStream);
if (playbackInputStream == null)
{
shutDown("Unable to convert stream of format " + audioInputStream + " to format " + format);
return;
}
// define the required attributes for our line,
// and make sure a compatible line is supported.
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
if (!AudioSystem.isLineSupported(info))
{
shutDown("Line matching " + info + " not supported.");
return;
}
// get and open the source data line for playback.
try
{
line = (SourceDataLine) AudioSystem.getLine(info);
line.open(format, bufSize);
}
catch (LineUnavailableException ex)
{
shutDown("Unable to open the line: " + ex);
return;
}
// play back the captured audio data
int frameSizeInBytes = format.getFrameSize();
int bufferLengthInFrames = line.getBufferSize() / 8;
int bufferLengthInBytes = bufferLengthInFrames * frameSizeInBytes;
byte[] data = new byte[bufferLengthInBytes];
int numBytesRead = 0;
// start the source data line
line.start();
while (this != null)
{
try
{
if ((numBytesRead = playbackInputStream.read(data)) == -1)
{
break;
}
int numBytesRemaining = numBytesRead;
while (numBytesRemaining > 0 )
{
numBytesRemaining -= line.write(data, 0, numBytesRemaining);
}
}
catch (Exception e)
{
shutDown("Error during playback: " + e);
break;
}
}
// we reached the end of the stream.
let the data play out, then
// stop and close the line.
if (this != null)
{
line.drain();
}
line.stop();
line.close();
line = null;
shutDown(null);
}
public void createAudioInputStream(File file)
{
if (file != null && file.isFile())
{
try
{
audioInputStream = AudioSystem.getAudioInputStream(file);
fileName = file.getName();
duration =(long)((audioInputStream.getFrameLength() * 1000) /audioInputStream.getFormat().getFrameRate());
}
catch (Exception ex)
{
// reportStatus(ex.toString());
}
}
else
{
// reportStatus("Audio file required.");
}
}
public static void main(String [] args)
{
Playback pb = new Playback("untitled");
// here you should set your soundfilename
pb.start();
for(int i = 0; i<3; i++)
// for example you can repeat 3 times
{
while(pb.isAlive())
;
if(pb == null)
{
pb = new Playback("untitled");
pb.start();
}
else
pb.run();
}
}
}