Ran*_*mar 2 android android-audiorecord android-security
我们正在开发实时流媒体视频应用程序。
所以我们需要为音频和视频内容提供安全保障。
我的尝试
我可以在以下代码的帮助下限制屏幕截图和视频内容
activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
但我无法通过其他应用程序限制录音。
如何限制其他应用的录音?
我从来没有听说过 Android 中有这样的官方工具可以简化这个过程。
但我认为您可以指出另一个应用程序录制音频。为此,请尝试在您的代码中使用MediaRecorder。例如,您将使用 Microphone ( MediaRecorder.AudioSource.MIC ) 作为输入源创建其实例。作为其他应用程序正在使用 MIC 的指示符,您将在开始录制时捕获异常(mRecorder.start())。如果不捕获异常,MIC 硬件什么时候可以免费使用。所以现在没有人录制音频。这个想法是你应该在每次你的应用程序进入前台时进行检查。例如在onResume () 或onStart()生命周期回调中。例如:
@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.
}
Run Code Online (Sandbox Code Playgroud)
您的代码将无法限制其他应用程序进行录制。你的try-catch块只会表明 MIC 正忙。并且您应该要求用户停止此操作,因为它是被禁止的。并且在 MIC 免费之前不要恢复流式传输。
示例如何使用 MediaRecorder 在这里。
正如我们在docs中看到的,MediaRecorder.start() 在以下情况下抛出异常:
投掷
IllegalStateException 如果在 prepare() 之前调用或当相机已被另一个应用程序使用时调用。
我在我的样本中尝试了这个想法。当一个应用程序获得 MIC 时,另一个应用程序无法使用 MIC。
优点:
这可以是一个工作工具:-))
缺点:
您的应用应请求RECORD_AUDIO权限。这可能会吓到用户。
我想重复一遍,这只是一个想法。