我的算法导致跳过和抖动......
以下是一些已知的事实:
audioQ是一个ArrayBlockingQueue [1]
当我减少encodeSample的运行时,此算法有效(例如在WAV的情况下)
当我在encodeSample(OGG编码)中实际执行操作时,此算法会产生抖动,跳过声音文件。
以下是一些代码:
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();
}
}
}
}
}