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

  • SEARCH
  • Home
  • 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 7829557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:49:30+00:00 2026-06-02T10:49:30+00:00

I’ve been working on an app that could playback audio from the microphone in

  • 0

I’ve been working on an app that could playback audio from the microphone in real time.
It sets up an AudioRecorder, that inits without any errors. However, it just returns a bunch of zeors, or a lot of number close to the max value of short, when performing read operations. I’m really stuck, it would be very kind if anyone could help me. This is my code:

public class AudioIn extends Thread {
public static final int ERROR_RECORD_INIT = -1;
public static final int ERROR_RECORD_NOTIFICATION = -2;
public static final int ERROR_RECORD_READ = -3;
public static final int SUCCESS = 0;

public static final int audioFrequency = 44100;
public static final int channelConfig = AudioFormat.CHANNEL_IN_MONO;
public static final int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
final int ShortsReadPerCycle = 1024;


private boolean capture = true;
private AudioRecord recorder;
private int effectiveCaptureBufferSize;
private short[] buffer;

private AudioInHandler handler;

public AudioIn()
{
    int minDeviceBuffer = AudioRecord.getMinBufferSize(audioFrequency, channelConfig, audioFormat);
    Log.d("AudioIn", "Minimum device capture buffer is: " + Integer.toString(minDeviceBuffer) + " bytes");

    effectiveCaptureBufferSize = minDeviceBuffer;
    Log.d("AudioIn", "Setting capture buffer size to " + effectiveCaptureBufferSize + " bytes");
}

public void close()
{
    capture = false;
}

public int samplesPerBuffer()
{
    return effectiveCaptureBufferSize / 2;
}

@Override
public void run() {
    // TODO Auto-generated method stub
    android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_AUDIO);
    try
    {
        recorder = new AudioRecord(AudioSource.MIC, audioFrequency, channelConfig, audioFormat, effectiveCaptureBufferSize);
    }
    catch (Throwable t)
    {
        handler.onError(ERROR_RECORD_INIT);
        return;         
    }

    recorder.startRecording();

    while(capture)
    {
        buffer = new short[ShortsReadPerCycle];
        int shortsRead = recorder.read(buffer, 0, buffer.length);
        if (shortsRead < 0)
        {
            new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    handler.onError(ERROR_RECORD_READ);
                }
            }.run();
            this.close();
        }
        else
        {
            new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    handler.onNewData(buffer);
                }
            }.run();    
        }
    }
    Log.d("AudioIn.run()", "Stopping AudioRecord...");
    recorder.stop();
    Log.d("AudioIn.run()", "Stopped AudioRecord, now releasing...");
    recorder.release();
    Log.d("AudioIn.run()", "AudioRecord released");
    recorder = null;
}

public void setHandler(AudioInHandler handler) {
    this.handler = handler;
}
}
  • 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-02T10:49:32+00:00Added an answer on June 2, 2026 at 10:49 am

    Calling read() on AudioRecord once does not ensure the whole short[] to be filled. You should check the returned “shortsRead” for actual short read and keep calling read() until the short[] is filled. Sample code given below.

    private void readFully(short[] data, int off, int length) {
            int read;
            while (length > 0) {
                read = mRec.read(data, off, length);
                length -= read;
                off += read;
            }
    }
    

    In your recording loop

    while (!released) {
        // fill the pktBuf
        short[] pktBuf = new short[pktSize];
        readFully(pktBuf, 0, pktSize);
        // Do something
    }
    

    In this way, everytime a call to read() is made, we increment the offset by “read” and decrement the length remaining by “read”, keep reading until the length remaining reaches 0. You will then get a filled short[] with recorded audio data.

    Similarly, when you are writing data into an AudioTrack, you have to do the same thing “writeFully()”, to ensure the whole short[] is written into the AudioTrack.

    Hope it helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a
I have a French site that I want to parse, but am running into

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.