Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to record audio/voice in background continuously in android?

Tags:

android

As I want to record audio in background I use service..But I am not able to record the audio in service.

I tried same code in Activity it works for me. but how to do audio recording in background when voice /speech is input that means audio recording should start if there is voice input and that should be in background...?

like image 936
Dipali Avatar asked Apr 05 '12 09:04

Dipali


People also ask

How do I record audio secretly on Android?

How to record conversation secretly. To record sound secretly on your Android device, install the secret voice recorder app from the Google Play Store. Now, whenever you need to record audio secretly, just press the power button thrice within 2 seconds to start recording.

How can I record my voice with background?

So how do you record yourself singing with background music? To record songs with background music, first, download a karaoke app like SingPlay. Then find your chosen track to download to the app. Finally, open your track through the app and then press record to sing over the track whilst it plays.

How do you repeat a recording on Android?

If that's what you want to do, tap on the circle button to start recording. But, if you have a video in your gallery that you want to edit and loop, tap on the folder icon at the bottom right. Search through your device's files and tap on the video you want to loop.


2 Answers

In one of my project I had this requirement to continuously record audio from Microphone. I can not share the project but I can share the specific AudioRecorder class.

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import android.content.Context;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.util.Log;

    public class AudioRecorder {
        public enum State {
            INITIALIZING,
            READY,
            RECORDING,
            ERROR,
            STOPPED
        };
        private byte[] audioBuffer = null;
        private int source = MediaRecorder.AudioSource.MIC;
        private int sampleRate = 0;
        private int encoder = 0;
        private int nChannels = 0;
        private int bufferRead = 0;
        private int bufferSize = 0;
        private RandomAccessFile tempAudioFile = null;
        public AudioRecord audioRecorder = null;
        private State state;
        private short bSamples = 16;
        private int framePeriod;

        // The interval in which the recorded samples are output to the file
        // Used only in uncompressed mode
        private static final int TIMER_INTERVAL = 120;
        volatile Thread t = null;
        public int TimeStamp = 0, count = 0, preTimeStamp = 0;

        public AudioRecorder(Context c) {
            this.sampleRate = 11025;
            this.encoder = AudioFormat.ENCODING_PCM_16BIT;
            this.nChannels = AudioFormat.CHANNEL_CONFIGURATION_MONO;
            this.preTimeStamp = (int) System.currentTimeMillis();
            myApp = (MyApp) c.getApplicationContext();
            mQueue = myApp.getQueue();

            try {
                /*          
                    String fileName = "/sdcard/XYZ/11025.wav";
                    tempAudioFile = new RandomAccessFile(fileName,"rw");
                */

                framePeriod = sampleRate * TIMER_INTERVAL / 1000;
                bufferSize = framePeriod * 2 * bSamples * nChannels / 8;

                if (bufferSize < AudioRecord.getMinBufferSize(sampleRate, nChannels, encoder)) {
                    bufferSize = AudioRecord.getMinBufferSize(sampleRate, nChannels, encoder);

                    // Set frame period and timer interval accordingly
                    framePeriod = bufferSize / (2 * bSamples * nChannels / 8);
                    Log.w(AudioRecorder.class.getName(), "Increasing buffer size to " + Integer.toString(bufferSize));
                }

                audioRecorder = new AudioRecord(source, sampleRate, nChannels, encoder, bufferSize);
                audioBuffer = new byte[2048];
                audioRecorder.setRecordPositionUpdateListener(updateListener);
                audioRecorder.setPositionNotificationPeriod(framePeriod);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        private AudioRecord.OnRecordPositionUpdateListener updateListener = new AudioRecord.OnRecordPositionUpdateListener() {
            @Override
            public void onPeriodicNotification(AudioRecord recorder) {
                //          Log.d(Constant.APP_LOG,"Into Periodic Notification...");
            }
            catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            */
        }

        @Override
        public void onMarkerReached(AudioRecord recorder) {
            // TODO Auto-generated method stub
        }
    };

    public void start() {
        if (state == State.INITIALIZING) {
            audioRecorder.startRecording();
            state = State.RECORDING;

            t = new Thread() {
                public void run() {
                    //Here You can read your Audio Buffers
                    audioRecorder.read(audioBuffer, 0, 2048);
                }
            };

            t.setPriority(Thread.MAX_PRIORITY);
            t.start();
        } else {
            Log.e(AudioRecorder.class.getName(), "start() called on illegal state");
            state = State.ERROR;
        }
    }

    public void stop() {
        if (state == State.RECORDING) {
            audioRecorder.stop();
            Thread t1 = t;
            t = null;
            t1.interrupt();
            count = 0;
            state = State.STOPPED;
        } else {
            Log.e(AudioRecorder.class.getName(), "stop() called on illegal state");
            state = State.ERROR;
        }
    }

    public void release() {
        if (state == State.RECORDING) {
            stop();
        }

        if (audioRecorder != null) {
            audioRecorder.release();
        }
    }

    public void reset() {
        try {
            if (state != State.ERROR) {
                release();
            }
        } catch (Exception e) {
            Log.e(AudioRecorder.class.getName(), e.getMessage());

            state = State.ERROR;
        }
    }

    public State getState() {
        return state;
    }
}

Now, Create Service and just call start() method and manipulate your recorded audio buffer for your purpose.

Hope it will Help you.

like image 82
N.Droid Avatar answered Oct 04 '22 03:10

N.Droid


For starting the recording in backgroun you can either

  • create a thread and do the recording inside a thread.

  • create a service which will run in background.

Hope it helps.

Edit 1

Thread recordInBackGround= new Thread(new Runnable() {
                    @Override
                    public void run() { 
//Your recording portion of the code goes here.
}
});

recordInBackGround.start();
like image 38
Deva Avatar answered Oct 04 '22 03:10

Deva