1 46 package sample.blocks.stream.file; 47 48 import java.io.BufferedInputStream ; 49 import java.io.IOException ; 50 import java.io.InputStream ; 51 52 53 import javax.sound.midi.InvalidMidiDataException ; 54 import javax.sound.midi.MidiUnavailableException ; 55 56 import javax.sound.sampled.AudioFormat ; 57 import javax.sound.sampled.AudioInputStream ; 58 import javax.sound.sampled.AudioSystem ; 59 import javax.sound.sampled.DataLine ; 60 import javax.sound.sampled.SourceDataLine ; 61 62 import org.mr.api.blocks.MantaInputStream; 63 64 68 69 72 public class MediaStreamReceiver { 73 public static final int BLOCK_SIZE = 1024; 74 public static final String queueName="midi"; 75 76 public static void main(String [] args) throws Exception { 77 78 MantaInputStream feed = new MantaInputStream(); 79 feed.connect(queueName,MantaInputStream.QUEUE); 80 Thread.sleep(5000); 81 System.out.println("Ready to receive stream files on queue "+queueName); 82 BufferedInputStream buffFeed = new BufferedInputStream (feed); 83 stream(buffFeed); 84 85 } 87 public static void stream(InputStream in) 88 throws IOException , InvalidMidiDataException , MidiUnavailableException 89 { 90 91 AudioInputStream audioInputStream = null; 92 try 93 { 94 audioInputStream = AudioSystem.getAudioInputStream(in); 95 } 96 catch (Throwable t) 97 { 98 t.printStackTrace(); 99 System.exit(1); 100 } 101 102 103 AudioFormat audioFormat = audioInputStream.getFormat(); 104 105 106 SourceDataLine sourceDataLine = null; 107 DataLine.Info info = new DataLine.Info (SourceDataLine .class, 108 audioFormat); 109 try 110 { 111 sourceDataLine = (SourceDataLine ) AudioSystem.getLine(info); 112 113 sourceDataLine.open(audioFormat); 114 }catch (Throwable t) 115 { 116 t.printStackTrace(); 117 return; 118 } 119 120 121 sourceDataLine.start(); 122 123 124 int nBytesRead = 0; 125 byte[] abData = new byte[BLOCK_SIZE]; 126 while (nBytesRead != -1) 127 { 128 try 129 { 130 nBytesRead = audioInputStream.read(abData, 0, abData.length); 131 } 132 catch (IOException e) 133 { 134 e.printStackTrace(); 135 } 136 if (nBytesRead >= 0) 137 { 138 int nBytesWritten = sourceDataLine.write(abData, 0, nBytesRead); 139 } 140 } 141 142 145 sourceDataLine.drain(); 146 147 150 sourceDataLine.close(); 151 152 }} | Popular Tags |