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 8122499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:51:20+00:00 2026-06-06T05:51:20+00:00

I am using AudioRecord to record raw audio for processing. The audio records entirely

  • 0

I am using AudioRecord to record raw audio for processing.
The audio records entirely without any noise but when the raw PCM data generated is played back, it plays as if it has been speeded up a lot (upto about twice as much).
I am viewing and playing the PCM data in Audacity. I am using actual phone (Samsung Galaxy S5670) for testing.
The recording is done at 44100 Hz, 16 bit. Any idea what might cause this?

Following is the recording code:

public class TestApp extends Activity
{   
File file;
OutputStream os;
BufferedOutputStream bos;       
AudioRecord recorder;   
int iAudioBufferSize;
boolean bRecording; 
int iBytesRead;

Thread recordThread = new Thread(){
    @Override
    public void run()
    {
        byte[] buffer = new byte[iAudioBufferSize];
        int iBufferReadResult;
        iBytesRead = 0;
        while(!interrupted())
        {
            iBufferReadResult = recorder.read(buffer, 0, iAudioBufferSize);
            // Android is reading less number of bytes than requested.
            if(iAudioBufferSize > iBufferReadResult)
            {
                iBufferReadResult = iBufferReadResult + 
                        recorder.read(buffer, iBufferReadResult - 1, iAudioBufferSize - iBufferReadResult);
            }               
            iBytesRead = iBytesRead + iBufferReadResult;
            for (int i = 0; i < iBufferReadResult; i++)
            {
                try
                {
                    bos.write(buffer[i]);
                } catch (IOException e)
                {                                               
                    e.printStackTrace();
                }
            }
        }
    }
};  

@Override
public void onCreate(Bundle savedInstanceState)
{
    // File Creation and UI init stuff etc.

    bRecording = false;
    bPlaying = false;

    int iSampleRate = AudioTrack.getNativeOutputSampleRate(AudioManager.STREAM_SYSTEM);
    iAudioBufferSize = AudioRecord.getMinBufferSize(iSampleRate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT); 
    recorder = new AudioRecord(AudioSource.MIC, iSampleRate, AudioFormat.CHANNEL_IN_MONO, 
        AudioFormat.ENCODING_PCM_16BIT, iAudioBufferSize);

    bt_Record.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            if (!bRecording)
            {
                try
                {                       
                    recorder.startRecording();
                    bRecording = true;
                    recordThread.start();                       
                }
                catch(Exception e)
                {
                    tv_Error.setText(e.getLocalizedMessage());
                }
            }
            else
            {
                recorder.stop();
                bRecording = false;
                recordThread.interrupt();
                try
                {
                    bos.close();
                }
                catch(IOException e)
                {

                }
                tv_Hello.setText("Recorded Sucessfully. Total " + iBytesRead + " bytes.");
            }
        }

    });
}
}

RESOLVED : I posted this after struggling with it for 1-2 days. But, ironically, I found the solution soon after posting. The buffered output stream write was taking too much time in the for loop, so the stream was skipping samples. changed it to block write, removing the for loop. Works perfectly.

  • 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-06T05:51:22+00:00Added an answer on June 6, 2026 at 5:51 am

    The audio skipping was caused by the delay in writing to buffer.
    the solution is to just replace this FOR loop:

            for (int i = 0; i < iBufferReadResult; i++)
            {
                try
                {
                    bos.write(buffer[i]);
                } catch (IOException e)
                {                                               
                    e.printStackTrace();
                }
            }
    

    by a single write, like so:

           bos.write(buffer, 0, iBufferReadResult);
    

    I had used the code from a book which worked, I guess, for lower sample rates and buffer updates.

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

Sidebar

Related Questions

I try to record audio using android ndk. people say I can use frameworks/base/media/libmedia/AudioRecord.cpp.
I am using the AudioRecord class to analize raw pcm bytes as it comes
is any example for audio record using Audio-recorder...and if it is possible to develop
I am recording audio using AudioRecord class.I want to record audio into a particular
I have an app on the market that records audio using the AudioRecord class
I'm using AudioRecorder to record short audio clips but I'm getting IllegalStateException when calling
I am using a thread which records the audio using AudioRecord class and placed
Using C# How can I show a document in richtextbox without scrollbars, But I
I'm trying to develop an Audio Capture application using the AudioRecord class from android
I'm making an app that records uncompressed (wav format) audio. I'm using this class

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.