简体   繁体   中英

How to continue recording even after pressing the home button?

I'm programming an Android application (I'm using Android Studio ).

I start the recording with the following code (this is just a fragment):

public void start() {
    int minBufferSize = AudioRecord.getMinBufferSize(SAMPLE_RATE, CHANNELS, AUDIO_ENCODING);
    audioRecord = new AudioRecord(
            MediaRecorder.AudioSource.MIC,
            SAMPLE_RATE, CHANNELS, AUDIO_ENCODING,
            minBufferSize
    );
    audioRecord.startRecording();
    recordingThread = new Thread(new Runnable() {
        public void run() {
            readAudioData();
        }
    }, "AudioRecorder Thread");
    recordingThread.start();
}

I want, when I press the home button, the application continues recording. Right now, when I press the home button, the recording stops.

Any idea on how to do this?

You can to create a service and you can start the service when you want to record audio it works even you press home button.

You can start service by

startService(new Intent(YourActivity,Service.class));

and can call that method onStartCommand() and can stop service by this

stopService(new Intent(YourActivity,Service.class));

or by stopself();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM