How to disable audio recording apps in Android
Asked Answered
P

1

5

We are developing live streaming video application.

So we need to give secure for Audio & Video content.

What I am tried

I am able to restrict screenshots & video content with help of following code

activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);

But I can't restrict audio recording through other apps.

How to restrict the audio recording by other apps?

Phylloid answered 3/10, 2017 at 7:2 Comment(4)
I think it is neither possible nor useful. What if the user records the audio using external recording device?Copartner
@NabinBhandari External device we don't care. We want provide atleast some securities. Thats the reason for this question.Phylloid
If your application is playing in the background, there is nothing you can do to not allow applications from recording your audio.Ary
@JoxTraex when it goes to background we pause the player. We only want to restrict the audio in foreground appPhylloid
U
8

I've never heard of such official tools in Android that would simplify this process.

But I think that you can indicate that another app records audio. Try to use MediaRecorder in your code for this. For example you will create its instance with Microphone (MediaRecorder.AudioSource.MIC) as input source. As indicator that MIC is busy by other app, you will catch exception, when you starts recording (mRecorder.start()). If you will not catch exception, when MIC hardware is free to use. So no one is recording audio now. The idea is that you should do that check every time your app comes to foreground. For example in onResume() or onStart() lifecycle callback. E.g:

@Override
protected void onResume() {
  super.onResume();
  ...
  boolean isMicFree = true;
  MediaRecorder recorder = new MediaRecorder();
  recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
  recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
  recorder.setOutputFile("/dev/null");
  recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
  ...
  // Configure MediaRecorder
  ...
  try {
      recorder.start();
  } catch (IllegalStateException e) {
      Log.e("MediaRecorder", "start() failed: MIC is busy");

      // Show alert dialogs to user.
      // Ask him to stop audio record in other app.
      // Stay in pause with your streaming because MIC is busy.

      isMicFree = false;
  }

  if (isMicFree) {
    Log.e("MediaRecorder", "start() successful: MIC is free");
    // MIC is free.
    // You can resume your streaming.
  }
  ...
  // Do not forget to stop and release MediaRecorder for future usage
  recorder.stop();
  recorder.release();
}

// onWindowFocusChanged will be executed
// every time when user taps on notifications
// while your app is in foreground.

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    // Here you should do the same check with MediaRecorder.
    // And you can be sure that user does not
    // start audio recording through notifications.
    // Or user stops recording through notifications.
}

Your code will not be able to restrict other app from recording. Your try-catch block will just indicate that MIC is busy. And you should ask user to stop this action because it is forbidden. And do not resume streaming until MIC will be free.

Sample how to use MediaRecorder is here.

As we can see in docs, MediaRecorder.start() throws exception if:

Throws

IllegalStateException if it is called before prepare() or when the camera is already in use by another app.

I tried this idea in my samples. When one app acquires MIC, the other is not able to work with MIC.

  • Pros:

    this can be a working tool :-))

  • Cons:

    Your app should ask for RECORD_AUDIO permission. This can scare the user.

I want to repeat that this is just an idea.

Unlearn answered 10/10, 2017 at 6:6 Comment(13)
Hi Sheikh I just now tried your code. Both exception called. But I don't know what we do in this exception block. After implemented this code still able to record the sound.Phylloid
By the way, is this your case? Do you also want to restrict recording from microphone?Unlearn
Yes. I want to restrict all audio recording in my app.Phylloid
Also, I think this wouldn't work if another app has been recording already.Chinkiang
@EndreBörcsök, yes, that's right! I tried to explain this in answer when i edited it. I told about indication that another app has been recording alreadyUnlearn
yureka wow.. nice solution. I will offer my bounty to you.. But only one last help needed. . How to find MIC will be free? Because with your solution I can pause the player. But I need to resume when they stop recording.Phylloid
@RanjithKumar, user needs to switch from your app to another to stop recording. And this means that your app will catch onPause->...->onResume lifecycle callbacks. So, for example when user back to your app, you should again create MediaRecorder in your onResume. If MIC is free, when resume streaming. Something like that.Unlearn
@Unlearn ok understand. But How we find out MIC is free?Phylloid
@RanjithKumar, i updated sample code in my answer. Hope it helps you.Unlearn
@Unlearn thanks a lot.. Now I cant check. I will check & offer you my bounty tomorrow. one doubt nowdays most of the recording apps show the controls(record/stop/pause) in notification. Incase user stop the recording through notification --> onresume never called. any hack for this?Phylloid
@RanjithKumar, what about onWindowFocusChanged() callback? :) It will be executed if your activity loses and gains focus. This can help you when user will start/stop recording through notifications. I updated my answer.Unlearn
@RanjithKumar, i tried this same solution in my activity, But i am always getting "MIC is busy" as output..i am not running any app in background then also same output...what can i do to get this working? Why i am not getting "MIC is free" as output..Can you help meClime
Even I am getting Same...MIC is busy. Any change in version has caused this because it seems old answer.Frightful

© 2022 - 2024 — McMap. All rights reserved.