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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:09:15+00:00 2026-05-28T01:09:15+00:00

Here I am trying to do recording and playing a voice at a time,

  • 0

Here I am trying to do recording and playing a voice at a time, It is working fine for few seconds and force closing showing error at buffer writing to audiotrack(m_track.write(buffer, 0, BUF_SIZE);).
Please check the below code, and suggest whether I am initializing audio track object correctly.

    int SAMPLE_RATE = 8000;
    int BUF_SIZE = 1024;
    byte[] buffer = new byte[BUF_SIZE];

function()
{
try
{
            buffersize = AudioRecord
                    .getMinBufferSize(SAMPLE_RATE, AudioFormat.CHANNEL_IN_MONO,
                            AudioFormat.ENCODING_PCM_16BIT);

        } catch (Throwable t) {
            Log.e("Audio", "Buffer size failed");
        }

        if (buffersize <= BUF_SIZE) {
            buffersize = BUF_SIZE;
        }
        try {

            m_record = new AudioRecord(MediaRecorder.AudioSource.MIC,
                    SAMPLE_RATE, AudioFormat.CHANNEL_IN_MONO,
                    AudioFormat.ENCODING_PCM_16BIT, buffersize * 2);

            m_track = new AudioTrack(AudioManager.STREAM_VOICE_CALL,
                    SAMPLE_RATE, AudioFormat.CHANNEL_OUT_MONO,
                    AudioFormat.ENCODING_PCM_16BIT, buffersize * 2,
                    AudioTrack.MODE_STREAM);

            m_track.setPlaybackRate(SAMPLE_RATE);
        } catch (Throwable t) {
            Log.e("Audio", "Audio Failed");
        }

        m_record.startRecording();
        m_track.play();
        try {
            m_thread = new Runnable() {
                public void run() {
                    while (m_isRun) {

                        m_record.read(buffer, 0, BUF_SIZE);
                        m_track.write(buffer, 0, BUF_SIZE);
                    }
                }
            };

            m_thread.run();
        } catch (Exception e) {
            Log.e("ERROR", "exception: " + e.getLocalizedMessage());
        }

        Log.i("D2Record", "loopback exit");
    }
  • 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-05-28T01:09:16+00:00Added an answer on May 28, 2026 at 1:09 am

    I’m not exactly familiar with the topic but I think you’re having problems with buffer sizes. I’m leaving most of the code away and concentrate mainly on buffer handling.

    int SAMPLE_RATE = 8000;
    int BUF_SIZE = 1024;
    
    int buffersize = AudioRecord
                    .getMinBufferSize(SAMPLE_RATE, AudioFormat.CHANNEL_IN_MONO,
                            AudioFormat.ENCODING_PCM_16BIT);
    bufferSize = Math.Max(bufferSize, AudioTrack
                    .getMinBufferSize(SAMPLE_RATE, AudioFormat.CHANNEL_IN_MONO,
                            AudioFormat.ENCODING_PCM_16BIT);
    bufferSize = Math.max(bufferSize, BUF_SIZE);
    byte[] buffer = new byte[bufferSize];
    
    m_record = new AudioRecord(MediaRecorder.AudioSource.MIC,
                    SAMPLE_RATE, AudioFormat.CHANNEL_IN_MONO,
                    AudioFormat.ENCODING_PCM_16BIT, bufferSize);
    m_track = new AudioTrack(AudioManager.STREAM_VOICE_CALL,
                    SAMPLE_RATE, AudioFormat.CHANNEL_OUT_MONO,
                    AudioFormat.ENCODING_PCM_16BIT, bufferSize,
                    AudioTrack.MODE_STREAM);
    
    m_track.setPlaybackRate(SAMPLE_RATE);
    m_record.startRecording();
    m_track.play();
    while (m_isRun) {
        // You should check 'read' for errors after read.
        int read = m_record.read(buffer, 0, bufferSize);
        // You should check 'wrote' for errors after write.
        int wrote = m_track.write(buffer, 0, read);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying the following: CREATE TABLE Table1 ( RecordNo autonumber, --error here! PersonId varchar(50),
I am trying to use One-Key Macros (as was written here ) (defun toggle-kbd-macro-recording-on
I am going nuts here trying to resolve a cascading update/delete issue :-) I
I know of the ||= operator, but don't think it'll help me here...trying to
Here I am trying to implement a class for complex numbers using books and
Noob dumb question, no doubt -- but here it is: Trying to tutorialize myself
After my last, failed, attempt at asking a question here I'm trying a more
Here's the class im trying to store [Serializable] [XmlRoot(ElementName = Database, IsNullable = false,
Here is what I am trying to do: 1) Open an ofstream object in
Here is what I am trying to do: if (a==true) { dbA objectInstance =

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.