I'm trying to detect if MediaRecorder is used by other application, to stop my recorder and continue for later use. I saw Androids Voice recorder app, it does what exactly I'm talking about, but HOW ? Thank you.
Asked
Active
Viewed 3,860 times
4
-
Can you please elaborate why you want to know, I mean what is the specs ?? – Daud Arfin Jun 15 '12 at 12:05
-
Because, when I'm recording audio and calling in skype same time, my friend can't get my voice. I think it's problem of MediaRecorder, because when I'm not using it, everything works perfect. Thanks! – Nika Jun 15 '12 at 15:24
-
It seems what you really want is a bit trickier: to do what you seem to want, you would need, while recording, to determine if something else "wants" to use the MediaRecorder, and if so release yours before their attempt fails. If you have an example of an app that you think can do this, you might try running it against your own test MediaRecorder using app, and verify that it's actually releasing it to you, you might also try to figure out at what point in your app's preparation the other app is releasing it, by having your own app not complete the process and see if the other still stops. – Chris Stratton Jun 15 '12 at 16:20
-
That's what I'm trying to do, but the only proof I have is app only, not the source. Android's Audio Record stops itself when other app is trying to use MediaRecorder class (like Skype for example.) I'm trying to find a way to do such thing and can't find any. I'm trying to find such thing like 1 months and I'm really tired. Regards. – Nika Jun 16 '12 at 10:16
2 Answers
2
When another program is trying to access MediaRecorder, you either get an error or it crashes. In other words, you have exclusive access to it.
You can not get the state of MediaRecorder directly, however you can verify wheter you are recording. Here is the code that does what you want:
package com.aplayer;
import java.io.IOException;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.widget.Toast;
public class APlayerActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MediaRecorder
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
recorder.setOutputFile("/sdcard/test.wav");
try {
recorder.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
Log.d("ERROR ","IllegalStateException");
Toast msg = Toast.makeText(APlayerActivity.this, "IllegalStateException", Toast.LENGTH_LONG);
msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2, msg.getYOffset() / 2);
msg.show();
} catch (Exception e) {
// TODO Auto-generated catch block
Log.d("ERROR ","IOException");
e.printStackTrace();
Toast msg = Toast.makeText(APlayerActivity.this, "IOException", Toast.LENGTH_LONG);
msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2, msg.getYOffset() / 2);
msg.show();
}
try {
recorder.start();
Toast msg = Toast.makeText(APlayerActivity.this, "Recording", Toast.LENGTH_LONG);
msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2, msg.getYOffset() / 2);
msg.show();
} catch (Exception e) {
Log.e("ERROR", "start() failed");
Toast msg = Toast.makeText(APlayerActivity.this, "Recording failed", Toast.LENGTH_LONG);
msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2, msg.getYOffset() / 2);
msg.show();
}
}
}

Frank Anemaet
- 179
- 1
- 4
-
Hi, I'm new to android programming and if it matters, I'm not using the code in activity. I have class created for recording and using it with Timers to make them active. Here's class. http://pastebin.com/U9FBgZpi Thanks. – Nika Jun 15 '12 at 15:49
-
In the method public static void StartRecord(), you call the method: recorder.start();. Instead of calling it directly, put a try-catch around it, as shown in my code above, or it will crash when the mediarecorder is in use. – Frank Anemaet Jun 15 '12 at 16:38
-
You don't get me, I want to stop my apps recording when other software will try to use MediaRecorder, because my app just stops everything, what uses MediaRecorder class.. Thanks. – Nika Jun 15 '12 at 17:00
-
No app can use MediaRecorder while your program locked it. Thus, basically it is not possible. However, you could try some hacks, such as to release it for short periods of time or to stop recording based on certain programs in the process list. – Frank Anemaet Jun 15 '12 at 17:46
-
Then how android's Voice record app does it ? Whenever I'm calling to friend, Voice record app stops immediately and I'm able to talk with VoIP calls without any problems. There must be some way for that! – Nika Jun 15 '12 at 18:10
-
That is a workaround. For example if you run Voice Recorder, put it on record and then use Voice search you'll see it shows an Audio Error. Note that MediaRecord and AudioRecord don't have a pause function, see : http://stackoverflow.com/questions/8007682/how-to-pause-resume-a-recording-created-with-mediarecorder Thus I think the Voice Record program is listening some events. You can listen for incoming call events, I'm not sure if that includes VoIP calls: see thread http://stackoverflow.com/questions/7485482/how-to-identify-incoming-call-and-outgoing-call-in-android – Frank Anemaet Jun 15 '12 at 19:55
-
I just tested it, VoiceRecorder also pauses when you call the phone, thus it is listening for events. Upon a receival of a call event, it runs their custom pause function, when the call has ended it continues. – Frank Anemaet Jun 15 '12 at 19:59
-
Hi, I'm already using call events but it's not catching VoIP events. And about recording, when I'm calling to friend, recording is paused and mic is busy also, it means I can't talk and record same time. I've tried so many ways to do such thing but I failed.. Wish someone knew how to do that, it's really important for me. Thank you! – Nika Jun 16 '12 at 08:36
-
If there's no way with MediaRecorder, can it be done with AudioRecord class ? I need only result, I don't care about classes. Thanks! – Nika Jun 16 '12 at 10:52
1
public boolean isMicrophoneAvailable() {
boolean available = true;
AudioRecord recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, 44100, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_DEFAULT, 44100);
try {
if (recorder.getRecordingState() != AudioRecord.RECORDSTATE_STOPPED) {
available = false;
}
recorder.startRecording();
if (recorder.getRecordingState() != AudioRecord.RECORDSTATE_RECORDING) {
recorder.stop();
available = false;
}
recorder.stop();
} finally {
recorder.release();
}
return available;
}

mamun
- 11
- 1
-
please edit your answer and write some explanation for it to make it more clear. – Saeed Zhiany Oct 09 '19 at 12:57
-
duplication of https://stackoverflow.com/questions/35633513/how-to-check-whether-microphone-is-used-by-any-background-app – Ahmad Dec 13 '19 at 16:09