コード例 #1
0
  @Override
  public void run() {
    try {
      AudioFormat format = settings.getAudioFormat();
      DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, format);
      SourceDataLine speakers = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
      speakers.open(format);
      speakers.start();
      byte[] data = new byte[204800];

      while (!Thread.interrupted()) {
        int numBytesRead = receiver.getInputStream().read(data, 0, 1024);
        log.debug("[Player] received {} bytes", numBytesRead);
        speakers.write(data, 0, numBytesRead);
      }
    } catch (LineUnavailableException | IOException e) {
      log.error("[Player] error occurred", e);
    }
  }
コード例 #2
0
 public void run() {
   SourceDataLine line = null;
   try {
     try {
       line = (SourceDataLine) AudioSystem.getLine(new DataLine.Info(SourceDataLine.class, fmt));
       line.open(fmt, bufsize);
       line.start();
     } catch (Exception e) {
       e.printStackTrace();
       return;
     }
     byte[] buf = new byte[1024];
     //noinspection InfiniteLoopStatement
     while (true) {
       if (Thread.interrupted()) throw (new InterruptedException());
       synchronized (queuemon) {
         Collection<Runnable> queue = Audio.queue;
         Audio.queue = new LinkedList<Runnable>();
         for (Runnable r : queue) r.run();
       }
       synchronized (ncl) {
         for (CS cs : ncl) clips.add(cs);
         ncl.clear();
       }
       fillbuf(buf, 0, 1024);
       //noinspection StatementWithEmptyBody
       for (int off = 0; off < buf.length; off += line.write(buf, off, buf.length - off)) ;
     }
   } catch (InterruptedException ignored) {
   } finally {
     synchronized (Audio.class) {
       player = null;
     }
     if (line != null) line.close();
   }
 }