I'm designing a background audio recording code like following `
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(outputformat);
mediaRecorder.setAudioEncoder(audioencoder);
mediaRecorder.setOutputFile(mRecFile.getAbsolutePath());
mediaRecorder.prepare();
mediaRecorder.start();
Thread.sleep(mDuration);
mediaRecorder.stop();
mediaRecorder.reset();
mediaRecorder.release();
This code is in a Thread's run(), and the thread would be started when a service is bond by the main activity. If I leave the activity by press "back" key(Would release the bind), I found that the recording would continue. But, when I set the mDuration with a very big num(3600000 (1 hour)), the recording would stop after a while, and the mediaRecorder.stop();
would never execute.
If I don't leave the activity. The recording would always stopped normally. What I want to know is why can't I do a background sound recording for a long time?
You should use a
ForegroundService
to recored audio in background.You can get more info on Android Foreground Service HERE