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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:22:08+00:00 2026-06-08T21:22:08+00:00

In my application I want to implement methods which record the sound if any

  • 0

In my application I want to implement methods which record the sound if any sound occurs.

Something similar like this app: Talking Tom Cat

So what should I have to do?
Is there any demo that listens for sound and records it if any sound occurs?

  • 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-08T21:22:09+00:00Added an answer on June 8, 2026 at 9:22 pm

    Try this code in your application…

    private int RECORDER_CHANNELS = AudioFormat.CHANNEL_CONFIGURATION_MONO;
    private int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;
    private int RECORDER_SAMPLERATE = 44100;
    private byte RECORDER_BPP = (byte) 16;
    
    private AudioRecord audioRecorder;
    
    public void arm() {
        // Get the minimum buffer size required for the successful creation of an AudioRecord object.
        int bufferSizeInBytes = AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE, RECORDER_CHANNELS,
                RECORDER_AUDIO_ENCODING);
    
        // Initialize Audio Recorder.
        audioRecorder = new AudioRecord(MediaRecorder.AudioSource.MIC, RECORDER_SAMPLERATE,
                RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING, bufferSizeInBytes);
    
        // Start Recording.
        audioRecorder.startRecording();
    
        int numberOfReadBytes = 0;
        byte audioBuffer[] = new byte[bufferSizeInBytes];
        boolean recording = false;
        float tempFloatBuffer[] = new float[3];
        int tempIndex = 0;
        int totalReadBytes = 0;
        byte totalByteBuffer[] = new byte[60 * 44100 * 2];
    
        // While data come from microphone.
        while (true) {
            float totalAbsValue = 0.0f;
            short sample = 0;
    
            numberOfReadBytes = audioRecorder.read(audioBuffer, 0, bufferSizeInBytes);
    
            // Analyze Sound.
            for (int i = 0; i < bufferSizeInBytes; i += 2) {
                sample = (short) ((audioBuffer[i]) | audioBuffer[i + 1] << 8);
                totalAbsValue += Math.abs(sample) / (numberOfReadBytes / 2);
            }
    
            // Analyze temp buffer.
            tempFloatBuffer[tempIndex % 3] = totalAbsValue;
            float temp = 0.0f;
            for (int i = 0; i < 3; ++i)
                temp += tempFloatBuffer[i];
    
            if ((temp >= 0 && temp <= 350) && recording == false) {
                Log.i("TAG", "1");
                tempIndex++;
                continue;
            }
    
            if (temp > 350 && recording == false) {
                Log.i("TAG", "2");
                recording = true;
            }
    
            if ((temp >= 0 && temp <= 350) && recording == true) {
                Log.i("TAG", "Save audio to file.");
    
                // Save audio to file.
                String filepath = Environment.getExternalStorageDirectory().getPath();
                File file = new File(filepath, "AudioRecorder");
                if (!file.exists())
                    file.mkdirs();
    
                String fn = file.getAbsolutePath() + "/" + System.currentTimeMillis() + ".wav";
    
                long totalAudioLen = 0;
                long totalDataLen = totalAudioLen + 36;
                long longSampleRate = RECORDER_SAMPLERATE;
                int channels = 1;
                long byteRate = RECORDER_BPP * RECORDER_SAMPLERATE * channels / 8;
                totalAudioLen = totalReadBytes;
                totalDataLen = totalAudioLen + 36;
                byte finalBuffer[] = new byte[totalReadBytes + 44];
    
                finalBuffer[0] = 'R'; // RIFF/WAVE header
                finalBuffer[1] = 'I';
                finalBuffer[2] = 'F';
                finalBuffer[3] = 'F';
                finalBuffer[4] = (byte) (totalDataLen & 0xff);
                finalBuffer[5] = (byte) ((totalDataLen >> 8) & 0xff);
                finalBuffer[6] = (byte) ((totalDataLen >> 16) & 0xff);
                finalBuffer[7] = (byte) ((totalDataLen >> 24) & 0xff);
                finalBuffer[8] = 'W';
                finalBuffer[9] = 'A';
                finalBuffer[10] = 'V';
                finalBuffer[11] = 'E';
                finalBuffer[12] = 'f'; // 'fmt ' chunk
                finalBuffer[13] = 'm';
                finalBuffer[14] = 't';
                finalBuffer[15] = ' ';
                finalBuffer[16] = 16; // 4 bytes: size of 'fmt ' chunk
                finalBuffer[17] = 0;
                finalBuffer[18] = 0;
                finalBuffer[19] = 0;
                finalBuffer[20] = 1; // format = 1
                finalBuffer[21] = 0;
                finalBuffer[22] = (byte) channels;
                finalBuffer[23] = 0;
                finalBuffer[24] = (byte) (longSampleRate & 0xff);
                finalBuffer[25] = (byte) ((longSampleRate >> 8) & 0xff);
                finalBuffer[26] = (byte) ((longSampleRate >> 16) & 0xff);
                finalBuffer[27] = (byte) ((longSampleRate >> 24) & 0xff);
                finalBuffer[28] = (byte) (byteRate & 0xff);
                finalBuffer[29] = (byte) ((byteRate >> 8) & 0xff);
                finalBuffer[30] = (byte) ((byteRate >> 16) & 0xff);
                finalBuffer[31] = (byte) ((byteRate >> 24) & 0xff);
                finalBuffer[32] = (byte) (2 * 16 / 8); // block align
                finalBuffer[33] = 0;
                finalBuffer[34] = RECORDER_BPP; // bits per sample
                finalBuffer[35] = 0;
                finalBuffer[36] = 'd';
                finalBuffer[37] = 'a';
                finalBuffer[38] = 't';
                finalBuffer[39] = 'a';
                finalBuffer[40] = (byte) (totalAudioLen & 0xff);
                finalBuffer[41] = (byte) ((totalAudioLen >> 8) & 0xff);
                finalBuffer[42] = (byte) ((totalAudioLen >> 16) & 0xff);
                finalBuffer[43] = (byte) ((totalAudioLen >> 24) & 0xff);
    
                for (int i = 0; i < totalReadBytes; ++i)
                    finalBuffer[44 + i] = totalByteBuffer[i];
    
                FileOutputStream out;
                try {
                    out = new FileOutputStream(fn);
                    try {
                        out.write(finalBuffer);
                        out.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
    
                } catch (FileNotFoundException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
    
                // */
                tempIndex++;
                break;
            }
    
            // -> Recording sound here.
            Log.i("TAG", "Recording Sound.");
            for (int i = 0; i < numberOfReadBytes; i++)
                totalByteBuffer[totalReadBytes + i] = audioBuffer[i];
            totalReadBytes += numberOfReadBytes;
            // */
    
            tempIndex++;
        }
    }
    

    For more details you can also look at this demo…

    http://musicg.googlecode.com/files/musicg_android_demo.zip

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

Sidebar

Related Questions

I want to implement an application which will work as a parser. User will
I want to implement a simple application (modification of sample2) which is showing what
I have a simple solid modeling application in which I want to implement several
In my application i want to implement a trial and full version functionality for
I am working in an android application and I want to implement a Gallery.
I want to implement program by c# application. first of all I need web
I want to implement in my application the option to send the current user
I want to implement a job scheduler in my windows azure application. My aim
I want to implement mobile notification with for my mobile application for windows 7
I want to implement strictMode to make my application faster but whenver I try

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.