use of javax.sound.sampled.LineUnavailableException 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);
}
}
use of javax.sound.sampled.LineUnavailableException in project screenbird by adamhub.
the class AudioRecorder method openLine.
/**
* Opens this AudioRecorder's targetDataLine.
*/
public final void openLine() {
try {
DataLine.Info dataLineInfo = new DataLine.Info(TargetDataLine.class, audioFormat);
TargetDataLine tdl = (TargetDataLine) AudioSystem.getLine(dataLineInfo);
setTargetDataLine(tdl);
getTargetDataLine().open(audioFormat, tdl.getBufferSize());
getTargetDataLine().start();
audioThread = new AudioThread();
audioThread.start();
} catch (LineUnavailableException e) {
log(e);
} catch (Exception e) {
log(e);
}
}
use of javax.sound.sampled.LineUnavailableException in project openblocks by mikaelhg.
the class SoundManager method loadSound.
public static Sound loadSound(String soundFileName) {
final URL url = SoundManager.class.getResource(soundFileName);
if (url == null) {
System.out.println("Could not find resource " + soundFileName);
return null;
}
AudioInputStream audioInputStream;
try {
audioInputStream = AudioSystem.getAudioInputStream(url);
} catch (UnsupportedAudioFileException e) {
return null;
} catch (IOException e) {
return null;
}
final AudioFormat format = audioInputStream.getFormat();
final Clip clip;
try {
DataLine.Info info = new DataLine.Info(Clip.class, format);
clip = (Clip) AudioSystem.getLine(info);
clip.open(audioInputStream);
} catch (LineUnavailableException e) {
System.out.println("Sorry, sound is not available");
return null;
} catch (IOException e) {
return null;
}
return new Sound(clip);
}
use of javax.sound.sampled.LineUnavailableException in project d54 by mitrisdev.
the class AudioProcessor method openChannel.
/**
* Open the audio channel so data can be captured. This must be called once before frameUpdate or the
* data accessors are called.
*/
public void openChannel() {
try {
line.open();
} catch (LineUnavailableException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
line.start();
line.drain();
input = new AudioInputStream(line);
}
use of javax.sound.sampled.LineUnavailableException in project ACS by ACS-Community.
the class AlarmSound method play.
/**
* Play the sound for the given priority
*
* @param priority The priority of the alarm
*/
private void play(int priority) throws Exception {
if (priority < 0 || priority > 3) {
throw new IllegalStateException("Invalid alarm priority " + priority);
}
URL url = soundURLs[priority];
AudioInputStream audioInputStream = null;
try {
audioInputStream = AudioSystem.getAudioInputStream(url);
} catch (Throwable t) {
// If there is an error then the panel does nothing
// It might happen for example if another application
// is locking the audio.
System.err.println(t.getMessage());
t.printStackTrace();
return;
}
// Obtain the information about the AudioInputStream
AudioFormat audioFormat = audioInputStream.getFormat();
SourceDataLine line = null;
DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
// Get the list of available mixers
Mixer.Info[] mixersInfo = AudioSystem.getMixerInfo();
// one is available is found
for (int i = 0; i < mixersInfo.length && line == null; i++) {
Mixer.Info mi = mixersInfo[i];
try {
Mixer mixer = AudioSystem.getMixer(mi);
line = (SourceDataLine) mixer.getLine(info);
} catch (LineUnavailableException lue) {
System.err.println("Line unavailable " + lue.getMessage());
line = null;
continue;
} catch (Throwable t) {
System.err.println("Exception getting the line " + t.getMessage());
line = null;
continue;
}
try {
line.open(audioFormat, EXTERNAL_BUFFER_SIZE);
} catch (Throwable t) {
System.err.println("Error opeining the line: " + t.getMessage());
line = null;
continue;
}
try {
line.start();
} catch (Throwable t) {
System.err.println("Error starting the line: " + t.getMessage());
line = null;
continue;
}
try {
playOnLine(line, audioInputStream);
} catch (Throwable t) {
System.err.println("Error playing: " + t.getMessage());
line = null;
continue;
}
// plays what's left and and closes the audioChannel
line.drain();
line.close();
}
}
Aggregations