private void openOutput() throws IOException { AudioFormat audioFormat = new AudioFormat((float) vorbisInfo.rate, 16, vorbisInfo.channels, true, false); DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat, AudioSystem.NOT_SPECIFIED); if (!AudioSystem.isLineSupported(info)) { throw new IOException("line format " + info + "not supported"); } try { out = (SourceDataLine) AudioSystem.getLine(info); out.open(audioFormat); } catch (LineUnavailableException e) { throw new IOException("audio unavailable: " + e.toString()); } out.start(); updateVolume(volume); }
@Override public boolean play() { // ForPlay.log().info("play()"); if (playing) { // we have not yet received LineEvent.Type.STOP return false; } if (clip == null) { // no audio clip to play return false; } if (clip.isActive()) { // already playing return false; } try { inputStream = new FileInputStream(file); } catch (FileNotFoundException e) { ForPlay.log().warn("Sound file not found " + file); // give up return false; } AudioInputStream ais; try { // ForPlay.log().info("calling AudioSystem.getAudioInputStream()"); ais = AudioSystem.getAudioInputStream(inputStream); } catch (UnsupportedAudioFileException e) { ForPlay.log() .warn( "Failed to play sound " + file + " due to failure to get audio stream caused by " + e.toString(), e); return false; } catch (IOException e) { ForPlay.log() .warn( "Failed to play sound " + file + " due to failure to get audio stream caused by " + e.toString(), e); return false; } try { // ForPlay.log().info("calling clip.open()"); clip.open(ais); } catch (IOException e) { ForPlay.log() .warn( "Failed to play sound " + file + " due to failure to open clip caused by " + e.toString(), e); return false; } catch (LineUnavailableException e) { ForPlay.log() .info( "Not playing sound " + file + " due to failure to open clip caused by " + e.toString()); } catch (IllegalArgumentException e) { ForPlay.log() .info( "Not playing sound " + file + " due to failure to open clip caused by " + e.toString()); return false; } catch (IllegalStateException e) { // Line may already be open // TODO(fredsa): figure out why this happens ForPlay.log() .info( "Not playing sound " + file + " due to failure to open clip caused by " + e.toString()); return false; } if (looping) { clip.loop(Clip.LOOP_CONTINUOUSLY); } // ForPlay.log().info("calling clip.start()"); clip.start(); playing = true; return true; }