简体   繁体   中英

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.

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:

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:

            <com...CustomWebView
            android:id="@+id/webView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"

            />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM