0

我正在开发简单的生产者-消费者示例。一个线程使用类记录音频样本AudioRecord并将它们写入缓冲区。第二个只是读取缓冲区并且什么都不做。当用户想要停止记录时,第一个线程将特殊字符写入缓冲区,并指示另一个读取结束。这是我的代码

public class SpellCollectorActivity extends Activity implements OnClickListener{
  private ArrayBlockingQueue<byte[] > audioq;
  boolean needToBeStopped = false; 
  Button generate, action;

  private MyRecorder  rec;
  private MyReader mr;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    action = (Button) findViewById(R.id.actionButton);
    action.setOnClickListener(this);

    needToBeStopped = false;
    audioq = new ArrayBlockingQueue<byte[]>(CAPACITY);
}

public void onClick(View arg0){
    switch(arg0.getId()){
    case R.id.generateButton:
        generateContentToSpell();
        break;
    case R.id.actionButton:
        if(needToBeStopped){
            rec.stopThread();
            needToBeStopped = false;
            action.setText(this.getString(R.string.start));
        }else{
            rec = new MyRecorder(audioq);
            mr = new MyReader(audioq);
            rec.start();
            mr.start();
            needToBeStopped = true;
            action.setText(this.getString(R.string.stop));
        }
        break;
    }
}

  private class MyRecorder extends Thread{  
    private static final int freq = 22050;
    private static final int  channelConfiguration =    AudioFormat.CHANNEL_CONFIGURATION_MONO;
    private static final int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;

    private final BlockingQueue<byte[] > myRecAudioq;
    private AudioRecord recorder;
    private boolean recording = false;
    int bufferSize;

    /*konstruktor*/
    public MyRecorder(BlockingQueue<byte[]> q ){
        bufferSize =  AudioRecord.getMinBufferSize(freq, channelConfiguration, audioEncoding);
        myRecAudioq = q;
    }

    public void run(){
        recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, 
                   freq, channelConfiguration, 
                   audioEncoding, 3*bufferSize);
        recorder.startRecording();
        recording = true;
        byte[] buffer = new byte[bufferSize];
        while(recording){
            int readBufferSize = recorder.read(buffer, 0, bufferSize);
            if(readBufferSize>0){
                try {
                    myRecAudioq.put(buffer);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

    public void stopThread(){
        recorder.stop();
        recording = false;
        byte[] buffer = new byte[bufferSize];
        for(int i=0;i<bufferSize;i++){
            buffer[i] =(byte) 0xff;
        }
        try {
            myRecAudioq.put(buffer);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

   private class MyReader extends Thread{
    private final BlockingQueue<byte[]> bq;

    private static final int freq = 22050;
    private static final int  channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;
    private static final int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;

    private int counter = 0;

    public MyReader(BlockingQueue<byte[]> q){
        bq = q;
    }
    public void run(){
        int buffSize =  AudioRecord.getMinBufferSize(freq, channelConfiguration, audioEncoding);
        byte[] compareBuffer= new byte[buffSize];
        for(int i=0;i<buffSize;i++){
            compareBuffer[i] = (byte)0xff;
        }

        boolean reading = true;
        byte[] buffer = null;
        do{
            try {
                buffer = bq.take();
   reading = buffer.equals(compareBuffer);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            if(reading){
                int c=1;
            }
        }while(!reading);
        int a=5;
    }
  } 
 }

我试图在MyReader课堂上 设置断点,int c=1; 但从未达到。我使用这个例子编写了这段代码。可能是什么问题呢?

4

1 回答 1

0

'reading' 的值是 2 byte[] 之间的比较。

只有当它们都是同一个对象(指针,如果你愿意的话),也就是说,如果你第一次调用buffer = compareBuffer

What you actually want to do is compare all elements of the buffer, for example using the java.util.Arrays class :

reading = Arrays.equals(compareBuffer, buffer);
于 2012-09-04T10:11:14.027 回答