如何在Android中禁用音频录制应用

兰吉斯·库玛(Ranjith Kumar)

我们正在开发实时流视频应用程序。

因此,我们需要为音频和视频内容提供安全性。

我尝试过的

我可以在以下代码的帮助下限制屏幕截图和视频内容

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

但是我不能限制通过其他应用程序进行的音频录制。

如何限制其他应用程序的录音?

谢赫

我从未听说过Android中可以简化此过程的官方工具。

但是我认为您可以指示另一个应用程序记录了音频。为此请尝试在代码中使用MediaRecorder例如,您将使用麦克风(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.
}

您的代码将无法限制其他应用的录制。您的try-catch块仅表示MIC忙。并且您应该要求用户停止此操作,因为它被禁止。并且在MIC免费之前,不要恢复流媒体播放。

示例如何使用MediaRecorder在这里

正如我们在docs中看到的如果出现以下情况MediaRecorder.start()会引发异常:

投掷

IllegalStateException如果在prepare()之前调用此方法,或者当该摄像头已被其他应用程序使用时调用。

我在样本中尝试了这个想法。当一个应用程序获取MIC时,另一个应用程序将无法使用MIC。

  • 优点

    这可以是一个工作工具:-))

  • 缺点

    您的应用应请求RECORD_AUDIO权限。这会吓到用户。

我想重复一遍,这只是一个想法。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章