Mute webview in android apps
Asked Answered
F

1

6

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?

Ferrer answered 16/1, 2017 at 12:20 Comment(4)
what do you mean by mute a WebViewPorush
@Akshay Bhat 'AB' disable the sound volumeFerrer
please explain your requirement clearly. Post some codes then only we can help you.Clintonclintonia
@Gokul Sreenivasan it's clear! I want to disable (mute) the sound of a WebView you can think that WebView plays an audio and I'm gonna mute the WebView.Ferrer
S
4

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.

Sair answered 16/1, 2017 at 12:35 Comment(2)
This works, thanks. However it sets the volume setting of the entire phone, so I think you could make some users angry by doing this.Ticket
That's why you should restore the previous volume level upon user leaves your screen.Porush

© 2022 - 2024 — McMap. All rights reserved.