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?
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.