如何在Android中禁用录音应用 [英] How to disable audio recording apps in Android

查看:1075
本文介绍了如何在Android中禁用录音应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

We are developing live streaming video application.

因此,我们需要确保音频和音频的安全性。视频内容。

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

我的尝试

我可以限制屏幕截图&视频内容借助以下代码

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


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

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

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

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

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

推荐答案

我从来没有听说过Android可以简化此过程的官方工具。

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

但是我认为您可以表明另一个应用程序在记录音频。为此,请尝试在代码中使用 MediaRecorder
例如,您将使用麦克风( MediaRecorder.AudioSource.MIC )作为输入源来创建其实例。作为其他应用程序正在忙MIC的指示,当您开始录制( mRecorder.start() )时,您会捕获到异常。如果您不会捕获异常,则可以免费使用MIC硬件。因此,现在没有人在录制音频。
这个想法是,您应该在每次应用程序出现在前台时都进行检查。例如,在 onResume ()或 onStart()生命周期回调中。例如:

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

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

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.

使用MediaRecorder的示例为此处

Sample how to use MediaRecorder is here.

我们在文档 MediaRecorder.start()在以下情况下会引发异常:

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


抛出

IllegalStateException如果在准备之前调用()或其他应用程序已经使用了相机。

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.


  • 专业人士:当一个应用获得MIC时,另一个则无法使用MIC。

  • Pros:

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

this can be a working tool :-))

缺点 >:

您的应用应请求 RECORD_AUDIO 权限。

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

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

I want to repeat that this is just an idea.

这篇关于如何在Android中禁用录音应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆