aLearner
aLearner

Reputation: 1047

Check to see if Android MediaRecorder is Recording

I've written some code for Android 2.2 that plays an audio file using the Android MediaPlayer. Without getting into the details of the code, I noticed that there exists a function called

isPlaying()

that allows you to check if an audio file is currently being played by the MediaPlayer. So, for example, when the following snippet of code runs

Toast.makeText(getApplicationContext(), "Sound playing is: " +
        mediaPlayer.isPlaying(), Toast.LENGTH_SHORT).show();

it displays the following message

Sound playing is: true / false

depending on whether there's sound playing or not.

When I wrote some code to record sound from the microphone using the Android MediaRecorder, however, I noticed that there did not look like there exists a function called

isRecording()

that checks to see whether a recording is in progress.

So, I was wondering if the onus is on the programmer then to figure out if a recording is in progress by embedding some logic into their code - or if perhaps there indeed exists a way to do this (check if a recording is in progress) by using another in-built function offered by the Android API.

Upvotes: 3

Views: 6950

Answers (2)

Tarique Ali Mirza
Tarique Ali Mirza

Reputation: 81

Important

I found a nice workaround to handle this issue, and you can read the workaround right away, but I strongly suggest that you read the entire explanation first.

The scenario

I was trying to find a way to figure out if the mediarecorder had started recording or not, I assumed that calling the start() method on the recorder after the prepare() method was enough, but it turns out, it isn't.

Before you get offended by what I just said, Let me explain the scenario...

I was building a simple audio recording app from scratch, no libraries, copy paste, all hardwork. So I knew exactly what each part of my code is doing. Or at least I thought I did.

Until I decided to try and break my application by clicking on the buttons to start and stop recording like I was playing a piano. And yes, my stop button wasn't even appearing until the mediarecorder's start() method was called.

So I was greeted with a crash and logcat welcomed me with

java.lang.RuntimeException: stop failed.
    at android.media.MediaRecorder.stop(Native Method)

along with

E/MediaRecorder(15709): stop failed: -1007

So I read online and found out that calling stop() right after start() on MediaRecorder causes this problem.

So the biggest question was, how do I detect if it is now SAFE for me to enable the STOP button on my recorder?

The Workaround (Not at all perfect, but it works)

MediaRecorder.getMaxAmplitude() // The maximum absolute amplitude measured since the last call, or 0 when called for the first time

As you can see, the MediaRecorder.getMaxAmplitude() method or the MediaRecorder.maxAmplitude property return 0 when called for the first time and the amplitude after that.

So, instead of allowing the user to Stop the recording right after calling MediaRecorder.start() I am now waiting until the MediaRecorder.maxAmplitude value is greater than zero, at which point I can be sure that the MediaRecorder is initialized, started, and recording, and is in a state where calling stop() is allowed. You can accomplish this by using a runnable that keeps checking until the amplitude is greater than 0. I am already using a runnable for the timer, so I perform the check in that.

Please Note

When working on an emulator, the value returned by MediaRecorder.maxAmplitude is always 0. So, you should use an Android Device to check if everything works as expected.

Now my buttons stay disabled for less than a second when I first start recording. But if I stop and start too quickly, they remain disabled for a bit longer, and I show a "Please wait while the recording starts" message for the user.

I hope this answer helps someone.

Regards!

Upvotes: 4

aLearner
aLearner

Reputation: 1047

Doesn't look like there is such a function after all. I think it makes sense to try to embed some logic in the code to do this cleanly.

Upvotes: 3

Related Questions