Currently, I am still in the proccess of learning Android development, so please excuse me if this question of mine is not easily understandable.
I have created an Android app that use WebView and I want to know how to enable sound when I typing in WebView within my app.
So far I only able to enable the sound when clicking a website link in WebView.
Any help would be greatly appreciated.
This is my WebView code.
public class WebViewActivity extends AppCompatActivity {
private WebView webView;
String url = "www.google.com";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
webView = findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
websettings.setDomStorageEnabled(true);
websettings.setJavaScriptEnabled(true);
webView.getSettings().setAppCachePath(getApplicationContext().getFilesDir().getAbsolutePath() + "/cache");
webView.getSettings().setDatabasePath(getApplicationContext().getFilesDir().getAbsolutePath() + "/databases");
if (Build.VERSION.SDK_INT >= 21) {
CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);
} else {
CookieManager.getInstance().setAcceptCookie(true);
}
webView.loadUrl(url);
webView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
soundOn();
view.loadUrl(url);
return true;
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
}
...
}
...
private void soundOn() {
SoundPool sp = new SoundPool.Builder()
.setMaxStreams(5)
.build();
sp.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int i, int i1) {
soundPool.play(i, 1f, 1f, 0, 0, 1);
}
});
sp.load(getApplicationContext(), R.raw.mysound, 1);
}
}
1-You first create a custom class from web view like the one below:
}
2-Then go to layout and replace your WebView class: