1 19 package org.lucane.applications.audioconf.audio; 20 21 import java.io.*; 22 import javax.sound.sampled.*; 23 24 import org.lucane.applications.audioconf.AudioConf; 25 import org.xiph.speex.SpeexDecoder; 26 27 30 public class AudioPlayer implements Runnable 31 { 32 private SourceDataLine dataLine; 34 private AudioFormat targetFormat; 35 36 private AudioConfig audioConfig; 37 private InputStream source; 38 39 private AudioConf plugin; 40 41 47 public AudioPlayer(AudioConf plugin, AudioConfig config, InputStream source) 48 { 49 this.plugin = plugin; 50 this.audioConfig = config; 51 this.source = source; 52 this.targetFormat = config.createAudioFormat(AudioFormat.Encoding.PCM_SIGNED); 53 initDataLine(); 54 } 55 56 59 private void initDataLine() 60 { 61 DataLine.Info info = new DataLine.Info(SourceDataLine.class, targetFormat, AudioSystem.NOT_SPECIFIED); 62 try { 63 dataLine = (SourceDataLine) AudioSystem.getLine(info); 64 dataLine.open(targetFormat, dataLine.getBufferSize()); 65 } catch(LineUnavailableException e) { 66 e.printStackTrace(); 68 } 69 } 70 71 74 public void stop() 75 { 76 dataLine.flush(); 77 dataLine.stop(); 78 dataLine.close(); 79 } 80 81 84 public void run() 85 { 86 dataLine.start(); 87 88 int read = 1; 89 90 byte[] speex = new byte[audioConfig.getSpeexBufferSize()]; 91 byte[] pcm = new byte[audioConfig.getPcmBufferSize()]; 92 SpeexDecoder decoder = audioConfig.createDecoder(); 93 94 while (read != -1 && dataLine.isOpen()) 95 { 96 try { 97 read = source.read(speex, 0, speex.length); 98 decoder.processData(speex, 0, read); 99 100 int length = decoder.getProcessedDataByteSize(); 101 decoder.getProcessedData(pcm, 0); 102 103 if(length >= 0) 104 dataLine.write(pcm, 0, length); 105 106 } catch (Exception e) { 107 plugin.reportPlayerError(e); 108 break; 109 } 110 111 } 112 } 113 } 114 | Popular Tags |