Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8488411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:31:41+00:00 2026-06-10T21:31:41+00:00

I am developing simple producer-consumer example. One thread records audio samples using AudioRecord class

  • 0

I am developing simple producer-consumer example. One thread records audio samples using AudioRecord class and writes them into buffer. Second one just reads the buffer and does nothing. When user wants to stop recording first thread writes special characters into the buffer and its a indicator for the other that reading is over. Here is my code

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;
    }
  } 
 }

I tried to put a breakpoints in MyReader class at line
int c=1;
but it’ s never reached. I wrote this code using this example. What could be the problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T21:31:42+00:00Added an answer on June 10, 2026 at 9:31 pm

    The value of ‘reading’ is a comparison between 2 byte[].

    This can only be true if they both are the same object (pointer, if you will), that is, if you first call 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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing a simple pyramid application where I am using JQuery to do
Developing a simple game for the iPhone, what gives a better performance? Using a
im developing a simple Mxml application using flex 3. I have used simple text
I'm developing a simple cache functionality with EhCache. There is a generic base class
I'm developing a simple page with Symfony2, using Twig as template engine. I have
i am developing simple game application just using pan gesture and cgaffinetransform rotate application
I'm developing simple android application, using Media Player to play sound. It has a
So, i am developing simple web application using Spring Framework. The application users can
I am developing a simple app. I am using sqlite to save data into
I'm developing simple survey service. I collect statistics in backend instance and dump them

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.