Mute webview in android apps

邮差的信 提交于 2019-12-23 17:30:44

问题


I'm building an app that includes some WebViews.

I want to mute one of them (not the whole app or device just that specific WebView) I searched but there's no certain answer to this question. Is there anyone knows how to mute a WebView sound in Android?


回答1:


You can't mute only WebViews volume, but you can mute the whole system volume when you are showing the WebView. Like :
When you are showing that particular WebView use the below method :

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);
}

And when the webView is not shown set max volume Like :

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);
}

If you don't want to set full volume you can get current system volume by

mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);

and save it locally and set it back again when you exit the Webview.



来源:https://stackoverflow.com/questions/41676323/mute-webview-in-android-apps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!