如何在安卓系统中禁用音频录制应用程序

5

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

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

我所尝试的

我能够通过以下代码帮助限制屏幕截图和视频内容:

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

但我无法限制其他应用程序通过音频录制。

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


我认为这既不可能也没有用。如果用户使用外部录音设备记录音频会怎样呢? - Nabin Bhandari
@NabinBhandari 外部设备我们不关心。我们希望至少提供一些安全性。这就是为什么有这个问题的原因。 - Ranjithkumar
如果您的应用程序正在后台运行,那么您无法阻止其他应用程序录制您的音频。 - JoxTraex
@JoxTraex 当应用进入后台时,我们会暂停播放器。我们只想限制前台应用程序中的音频。 - Ranjithkumar
1个回答

8

我从未听说过Android中有这样的官方工具,可以简化这个过程。

但是我认为你可以指示另一个应用程序记录音频。尝试在您的代码中使用MediaRecorder。例如,您将创建其实例,并将麦克风(MediaRecorder.AudioSource.MIC)作为输入源。作为指示器,其他应用程序正在使用麦克风时,您将捕获异常,当您开始录制(mRecorder.start())时。如果您没有捕获异常,则表示麦克风硬件可供使用。因此现在没有人正在录制音频。 这个想法是您应该每次应用程序进入前台时进行检查。例如,在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的示例可以在此处找到。
正如我们在文档中看到的那样,MediaRecorder.start()会抛出异常,如果:
IllegalStateException如果在prepare()之前调用它或相机已经被另一个应用程序使用。
我在我的样本中尝试了这个想法。当一个应用程序获取MIC时,另一个应用程序无法使用MIC。
优点:
这可能是一个有效的工具 :-))
缺点:
您的应用程序应请求RECORD_AUDIO权限。这可能会吓到用户。

我想重申这只是一个想法。


嗨,Sheikh,我刚刚尝试了你的代码。两个异常都被调用了。但是我不知道在这个异常块中该怎么做。实现了这段代码后仍然能够录制声音。 - Ranjithkumar
顺便问一下,这是你的情况吗?你也想限制麦克风录音吗? - Sheikh
哇,太棒了!你的解决方案很好。我会把我的赏金给你。但是还需要最后一点帮助。如何找到免费的MIC?因为使用你的解决方案,我可以暂停播放器。但是当他们停止录制时,我需要恢复播放。 - Ranjithkumar
@RanjithKumar,用户需要切换到另一个应用程序才能停止录制。这意味着您的应用程序将捕获 onPause -> ... -> onResume 生命周期回调。因此,例如,当用户返回到您的应用程序时,您应该在 onResume 中再次创建 MediaRecorder。如果MIC空闲,则恢复流式传输。就像这样。 - Sheikh
@RanjithKumar,我在我的答案中更新了示例代码。希望它能对你有所帮助。 - Sheikh
显示剩余8条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接