在 WebView 中键入时播放声音 Android
Play Sound When Typing in WebView Android
目前我还在学习Android开发中,如果我的这个问题不太容易理解,还请见谅。
我创建了一个使用 WebView 的 Android 应用程序,我想知道如何在我的应用程序中输入 WebView 时启用声音。
到目前为止,我只能在 WebView 中单击网站 link 时启用声音。
如有任何帮助,我们将不胜感激。
这是我的 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-您首先从网络视图创建自定义 class,如下所示:
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-然后转到布局并替换您的 WebView class:
<com...CustomWebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
目前我还在学习Android开发中,如果我的这个问题不太容易理解,还请见谅。
我创建了一个使用 WebView 的 Android 应用程序,我想知道如何在我的应用程序中输入 WebView 时启用声音。
到目前为止,我只能在 WebView 中单击网站 link 时启用声音。
如有任何帮助,我们将不胜感激。
这是我的 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-您首先从网络视图创建自定义 class,如下所示:
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-然后转到布局并替换您的 WebView class:
<com...CustomWebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>