How can handle click on audio link saved with .mht format in android studio webview?

219 views Asked by At

I'm a beginner in android programming and I have a problem.I have a webview in my layout that load a remote web page and I saved that with "webView.saveWebArchive(file.getAbsolutePath());" method, this page have multi audio link that click on it will played. but when I saved with mht format and reload it from local storage the audio link dosent work. and I don't know how can handle it. please help me!!

This is my Code:


private void InitialWebView() {
    webView = (WebView) findViewById(R.id.webloder);
    webView.getSettings().setSupportZoom(true);
    webView.getSettings().setBuiltInZoomControls(true);
    webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    webView.setScrollbarFadingEnabled(true);
    webView.getSettings().setLoadsImagesAutomatically(true);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.getSettings().setSupportMultipleWindows(true);
    webView.getSettings().setSupportMultipleWindows(true);
    webView.getSettings().setAllowFileAccess(true);
    webView.getSettings().setAppCacheEnabled(true);
    webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

    if (isSavedWord()) {
        webView.loadUrl("file:///" + android.os.Environment.getExternalStorageDirectory() +
                "/Ajhang Leitner Box/Web Pages/" + Argu[1] + "/" + Argu[0] + ".mht");
    } else
        webView.loadUrl(htmlPageUrl + Argu[0]);

    webView.setWebViewClient(new WebViewClient() {
        @Override
        public void onLoadResource(WebView view, String url) {
            if (isMp3(url)) {
                if (!PlayFromStorage(getAudioName(url))) {
                    if (checkInternetConenction())
                        downloadAudio(url, getAudioName(url));
                } else {
                    webView.stopLoading();
                }
            }
        }
    });}

private Boolean PlayFromStorage(String fileName) {

    try {

        File cacheDir = new File(android.os.Environment.getExternalStorageDirectory(), "Ajhang Leitner Box/" + fileName);
        if (cacheDir.exists()) {

            try {
                String filePath = android.os.Environment.getExternalStorageDirectory() + "/Ajhang Leitner Box/" + fileName;
                webView.loadUrl("file://" + android.os.Environment.getExternalStorageDirectory() + "/Ajhang Leitner Box/" + fileName);
                MediaPlayer mediaPlayer;
                mediaPlayer = new MediaPlayer();
                mediaPlayer.setDataSource(filePath);
                mediaPlayer.prepare();
                mediaPlayer.start();
            }
            catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            catch (RuntimeException e)
            {


            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return true;
        } else
            return false;
    } catch (Exception e) {
        return false;
    }

}

private boolean isSavedWord() {

    File cacheDir = new File(android.os.Environment.getExternalStorageDirectory(), "/Ajhang Leitner Box/Web Pages/" + Argu[1] + "/" + Argu[0] + ".mht");
    if (cacheDir.exists())
        return true;
    return false;
}


private boolean isMp3(String url) {
    if (url.contains(".mp3"))
        return true;
    return false;
}
1

There are 1 answers

4
Steven On

Look at this site to see the supported audio formats: https://developer.android.com/guide/topics/media/media-formats.html