use of javax.sound.sampled.DataLine in project JMRI by JMRI.
the class JavaSoundAudioSource method bindAudioBuffer.
@SuppressWarnings("SleepWhileInLoop")
@Override
boolean bindAudioBuffer(AudioBuffer audioBuffer) {
// First check we've been initialised
if (!initialised) {
return false;
}
// Wait for AudioBuffer to be loaded, or 20 seconds
long startTime = System.currentTimeMillis();
while (audioBuffer.getState() != AudioBuffer.STATE_LOADED && System.currentTimeMillis() - startTime < 20000) {
try {
Thread.sleep(50);
} catch (InterruptedException ex) {
}
}
if (audioBuffer instanceof JavaSoundAudioBuffer && audioBuffer.getState() == AudioBuffer.STATE_LOADED) {
// Cast to JavaSoundAudioBuffer to enable easier access to specific methods
JavaSoundAudioBuffer buffer = (JavaSoundAudioBuffer) audioBuffer;
// Get a JavaSound DataLine and Clip
DataLine.Info lineInfo;
lineInfo = new DataLine.Info(Clip.class, buffer.getAudioFormat());
Clip newClip;
try {
newClip = (Clip) mixer.getLine(lineInfo);
} catch (LineUnavailableException ex) {
log.warn("Error binding JavaSoundSource (" + this.getSystemName() + ") to AudioBuffer (" + this.getAssignedBufferName() + ") " + ex);
return false;
}
this.clip = newClip;
try {
clip.open(buffer.getAudioFormat(), buffer.getDataStorageBuffer(), 0, buffer.getDataStorageBuffer().length);
} catch (LineUnavailableException ex) {
log.warn("Error binding JavaSoundSource (" + this.getSystemName() + ") to AudioBuffer (" + this.getAssignedBufferName() + ")" + ex);
}
if (log.isDebugEnabled()) {
log.debug("Bind JavaSoundAudioSource (" + this.getSystemName() + ") to JavaSoundAudioBuffer (" + audioBuffer.getSystemName() + ")");
}
return true;
} else {
log.warn("AudioBuffer not loaded error when binding JavaSoundSource (" + this.getSystemName() + ") to AudioBuffer (" + this.getAssignedBufferName() + ")");
return false;
}
}
use of javax.sound.sampled.DataLine in project screenbird by adamhub.
the class AudioCache method playAudio.
/**
* Thread for playing audio.
*/
private synchronized void playAudio() {
try {
// Start a new thread for playing audio
if (this.playThread != null) {
this.playThread = null;
}
if (this.cacheStream != null) {
// Release for reading
this.cacheStream.flush();
this.cacheStream.close();
this.cacheStream = null;
}
// Load audio cache
log(String.format("Loading audio cache %s %d", this.cacheFile.getAbsolutePath(), this.cacheFile.length()));
final FileInputStream input = new FileInputStream(this.cacheFile);
log("Loaded audio cache file with size" + input.available());
// Set up hardware for playback
try {
AudioInputStream audioStream = AudioSystem.getAudioInputStream(this.cacheFile);
DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioStream.getFormat());
dataLine = (SourceDataLine) AudioSystem.getLine(info);
} catch (IOException e) {
log(e);
} catch (UnsupportedAudioFileException e) {
log(e);
}
// Set up audio playback thread.
Thread runner = new Thread("Preview Audio Thread") {
@Override
public void run() {
try {
// Marks audio in playing state
isPlaying = true;
// Prep hardware for audio playback
dataLine.open();
dataLine.start();
// Read data for playing audio
int bytesRead = 0;
int buffSize = dataLine.getBufferSize();
byte[] data = new byte[buffSize];
// Compute total time of audio playback
totalTimeMS = (long) ((input.available() * 1000) / ((double) audioFormat.getFrameSize() * audioFormat.getSampleRate()));
log("TimeMS: " + (startTimeMS));
// Prep offsets for accurate audio playback
currOffset = (long) ((startTimeMS / 1000) * (double) audioFormat.getFrameSize() * audioFormat.getSampleRate());
currTimeMS = startTimeMS;
log(String.format("Seek to MS[%d] Bytes[%d] TotalBytes[%d]", startTimeMS, currOffset, input.available()));
// If not starting at begining of audio file
input.skip(currOffset);
// Play the entire audio
while ((bytesRead = input.read(data, 0, data.length)) != -1 && isPlaying) {
currOffset += bytesRead;
// Update current time of audio that is being played
currTimeMS = (long) ((currOffset * 1000) / ((double) audioFormat.getFrameSize() * audioFormat.getSampleRate())) - 600;
// Check to see if sequence has been scrubbed
if (scrubIndex != null && !scrubIndex.isEmpty() && // Is current second in scrub index array
scrubIndex.indexOf((int) (currTimeMS / 1000)) >= 0) {
// Do not write to audio line
continue;
}
// Write to audio line
dataLine.write(data, 0, data.length);
}
if (isPlaying && dataLine != null) {
dataLine.drain();
dataLine.flush();
}
// Kills video feed
currTimeMS = totalTimeMS;
isPlaying = false;
if (dataLine != null) {
dataLine.stop();
}
log("Done with Audio");
} catch (LineUnavailableException e) {
log("No sound line available!" + e);
} catch (IOException e) {
log(e);
} finally {
// Release audio playback hardware
try {
dataLine.close();
} catch (NullPointerException e) {
// This always throws an exception for some reason
}
try {
input.close();
} catch (IOException e) {
log(e);
} catch (NullPointerException e) {
// Do nothing
}
}
// Stop running playback thread
this.interrupt();
playThread = null;
}
};
// Start audio playback thread
playThread = null;
playThread = runner;
playThread.start();
} catch (LineUnavailableException e) {
System.err.println("Line unavailable: " + e);
System.exit(-4);
} catch (FileNotFoundException e) {
log(e);
} catch (IOException e) {
log(e);
}
}
Aggregations