[英]Play Sound When Typing in WebView Android
Currently, I am still in the proccess of learning Android development, so please excuse me if this question of mine is not easily understandable.目前,我还在学习Android开发过程中,如果我的这个问题不太好理解,请见谅。
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.我创建了一个使用 WebView 的 Android 应用程序,我想知道如何在我的应用程序中输入 WebView 时启用声音。
So far I only able to enable the sound when clicking a website link in WebView.到目前为止,我只能在单击 WebView 中的网站链接时启用声音。
Any help would be greatly appreciated.任何帮助将不胜感激。
This is my WebView code.这是我的 WebView 代码。
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: 1-您首先从 Web 视图创建一个自定义类,如下所示:
public class CustomWebView extends WebView {
public CustomWebView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
return new BaseInputConnection(this, false); //this is needed for #dispatchKeyEvent() to be notified.
}
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
//return super.dispatchKeyEvent(event);
boolean dispatchFirst = super.dispatchKeyEvent(event);
// Listening here for whatever key events you need
if (event.getAction() == KeyEvent.ACTION_UP)
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_SPACE:
soundOn();
break;
case KeyEvent.KEYCODE_ENTER:
// e.g. get enter events here
break;
// case other keys
}
return dispatchFirst;
}
} }
2-Then go to layout and replace your WebView class: 2-然后转到布局并替换您的 WebView 类:
<com...CustomWebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.