wenqil
wenqil

Reputation: 11

Background audio record would stop itself

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?

Upvotes: 0

Views: 1585

Answers (1)

Vipul Purohit
Vipul Purohit

Reputation: 9827

You should use a ForegroundService to recored audio in background.

A foreground service is a service that's considered to be something the user is actively aware of and thus not a candidate for the system to kill when low on memory. A foreground service must provide a notification for the status bar, which is placed under the "Ongoing" heading, which means that the notification cannot be dismissed unless the service is either stopped or removed from the foreground.

You can get more info on Android Foreground Service HERE

Upvotes: 1

Related Questions