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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:51:40+00:00 2026-05-20T05:51:40+00:00

In my Application I want to use the AudioTrack class to play back some

  • 0

In my Application I want to use the AudioTrack class to play back some sound I record. I record the audio via an AudioRecord object, read the object and then I want to play the recorded sound. However, the way I have it programmed now will result in an error in the AudioTrack-JNI which will say that “Buffer direct access is not supported, can’t record”.

The code looks like this:

public class RecorderThread extends Thread{
    private static AudioRecord mAudioRecorder;
    private final int USE_MIC = MediaRecorder.AudioSource.MIC;
    private static final int FREQUENCY = 8000;
    private int mBuffSize, mChannel, mEncoding;
    private byte[] mBuffer;
    private Context mContext;
    private volatile boolean mContinueLoop = true,
                             mDataPresent = false,
                             mProcessData = false;

    private BroadcastReceiver mActionListener = new BroadcastReceiver(){
        @Override
        public void onReceive(Context context, Intent intent){
            if(intent.getAction().equals(mContext.getString(R.string.STOP_THREAD))){
                mContinueLoop = false;
                Thread.currentThread().interrupt();
            } else if(intent.getAction().equals(mContext.getString(R.string.START_RECORDING))){
                mAudioRecorder.startRecording();
            }  else if(intent.getAction().equals(mContext.getString(R.string.STOP_RECORDING))){
                mAudioRecorder.stop();
                mDataPresent = true;
            }  else if(intent.getAction().equals(mContext.getString(R.string.PROCESS_DATA))){
                if(mDataPresent){
                    mProcessData = true;
                    mDataPresent = false;
                }
            }  
        }
    };

    public RecorderThread(Context context){
        mContext = context;
        mChannel = AudioFormat.CHANNEL_IN_MONO;
        mEncoding = AudioFormat.ENCODING_PCM_16BIT;
        mBuffSize = AudioRecord.getMinBufferSize(FREQUENCY, mChannel, mEncoding);
        mBuffer = new byte[FREQUENCY * 10];
        mAudioRecorder = new AudioRecord(USE_MIC, FREQUENCY, mChannel, mEncoding, mBuffer.length);

        IntentFilter filter = new IntentFilter();
        filter.addAction(mContext.getString(R.string.START_RECORDING));
        filter.addAction(mContext.getString(R.string.STOP_RECORDING));
        filter.addAction(mContext.getString(R.string.PROCESS_DATA));
        filter.addAction(mContext.getString(R.string.STOP_THREAD));
        mContext.registerReceiver(mActionListener, filter);
    }

    @Override
    public void run(){
        while(mContinueLoop){
            if(mProcessData){
                // Process data.
                ByteBuffer sampledData = ByteBuffer.wrap(mBuffer);
                int readSamples = mAudioRecorder.read(sampledData, sampledData.limit());
                processData(sampledData, readSamples);
                mProcessData = false;
            }
            try{
                Thread.sleep(100);
            } catch(InterruptedException e){
                e.printStackTrace();
            }
        }
        mAudioRecorder.release();
    }

    private void processData(ByteBuffer sampledData, int readSamples) {
        byte[] data = sampledData.array();
        AudioTrack track = new AudioTrack(AudioManager.STREAM_MUSIC,
                                          FREQUENCY,
                                          AudioFormat.CHANNEL_OUT_MONO,
                                          mEncoding,
                                          data.length,
                                          AudioTrack.MODE_STATIC);
        if(track != null){
            track.write(data, 0, data.length);
            track.play();
            track.release();
        }
    }
}

I’ve looked through the native code that is makes the AudioTrack-JNI (http://www.netmite.com/android/mydroid/1.6/frameworks/base/core/jni/android_media_AudioRecord.cpp), but this doesn’t make it any clearer. The only times this error will pop up is when a buffer capacity <= 0 or when the address to this buffer is null.

So yeah, I’ve got no idea on why this doesn’t work… Anybody with a bit more experience might have… So please enlighten me? Thanks in advance…

  • 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-20T05:51:41+00:00Added an answer on May 20, 2026 at 5:51 am

    I’ve done some more debugging and found that the Issue is not the AudioTrack, but it is with the AudioRecord.read() method. Still haven’t got a solution on it though… Anybody?

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

Sidebar

Related Questions

In a Android application I want to use Scanner class to read a list
I have a non-rails application that I want to use rails active-record migrations with.
For my next application i want to use themes to have some eyecandy to
In my application I want to use the NSURLConnection class. So please tell me
I have a browser application and i want to use the Geolocation class. The
I want to write a web application and want to use Ruby. I have
In my Rails application I want to use the will_paginate gem to paginate on
I'm developing a web application I want to use the role authentication to control
In my Asp.net MVC 1.0 application I want to use CKEditor as my Rich
I want use JQuery mobile for the front-end of my mobile application, but I

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.