为什么我的Producer / Consumer线程算法导致音频跳过和抖动?

时间:2013-06-21 01:36:05

标签: java multithreading performance audio real-time

我的算法导致跳过和抖动......

以下是一些已知的事实:

  1. audioQ是一个ArrayBlockingQueue [1]

  2. 当我减少encodeSample的运行时,此算法有效(例如在WAV的情况下)

  3. 当我在encodeSample(OGG编码)中实际执行操作时,此算法会产生抖动,跳过声音文件。

  4. 以下是一些代码:

    public void putSample(short[] buf) throws IOException {
      try {
        long start = System.currentTimeMillis();
        audioQ.put(buf);
        long end = System.currentTimeMillis()-start;
        System.out.println(end);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }      
    
    
    class Consumer extends Thread{
      boolean isRunning;
      public Consumer(){
        System.out.println("Audio Consumer Started");
        this.isRunning=true;
      }
    
      @Override
      public void run() {
          System.out.println("Audio Consumer Running");
          short[] buf=null;
          //consuming messages until exit message is received
          while(isRunning) {
          try {
            if(audioQ.size() > 0) {
              System.out.println("AUDIOQ SIZE:"+audioQ.size());
            }
            buf = audioQ.take();
          } catch (InterruptedException e) {
            e.printStackTrace();
          } 
          if(buf != null) {
            try {
              encodeSample(buf);
            } catch (IOException e) {
              e.printStackTrace();
            }
          }
        }   
      }
    }
    
    1. http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/ArrayBlockingQueue.html

0 个答案:

没有答案