【问题标题】:Mute webview in android apps在 android 应用程序中静音 webview
【发布时间】:2017-01-16 12:20:21
【问题描述】:

我正在构建一个包含一些WebViews 的应用程序。

我想将其中一个静音(不是整个应用程序或设备,只是特定的WebView)我搜索过,但这个问题没有确定的答案。 有谁知道如何在 Android 中静音 WebView 声音?

【问题讨论】:

  • 静音WebView是什么意思
  • @Akshay Bhat 'AB' 禁用音量
  • 请清楚说明您的要求。发布一些代码,然后只有我们可以帮助您。
  • @Gokul Sreenivasan 很清楚!我想禁用(静音)WebView 的声音,您可以认为 WebView 播放音频,我将静音 WebView。

标签: android audio webview volume mute


【解决方案1】:

您不能只静音 WebViews 音量,但您可以在显示 WebView 时静音整个系统音量。喜欢 :
当您显示特定的 WebView 时,请使用以下方法:

public static void mute(Context context) {
    AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    int mute_volume = 0;
    mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, mute_volume, 0);
}

当 webView 不显示时,设置最大音量喜欢:

public static void unmute(Context context) {
    AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    int unmute_volume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, unmute_volume, 0);
}

如果您不想设置完整音量,您可以通过

获取当前系统音量
mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);

并在本地保存并在退出 Webview 时重新设置。

【讨论】:

  • 这行得通,谢谢。但是它设置了整个手机的音量设置,所以我认为这样做可能会让一些用户生气。
  • 这就是为什么您应该在用户离开屏幕时恢复之前的音量级别。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-15
  • 1970-01-01
  • 2012-11-12
相关资源
最近更新 更多