1

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 Answers1

0

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

Steven
  • 1,404
  • 2
  • 16
  • 39
  • It's dont mater to me play audio in mht file. I just want to know how can i handle when user touch on speaker button on saved web paged loaded by webview. – Ahmad Ansariyan Sep 19 '17 at 14:30
  • I don't understand what you mean? Your question is how to play .mht format. I say that is not possible. Now you say your are not interested in it. Please explain what you want. What code did you tried? – Steven Sep 19 '17 at 14:32
  • I display mht file with webview. But when i click on audio link it doesnt happen!!! – Ahmad Ansariyan Sep 19 '17 at 15:18
  • You don't need to shout just be friendly. Shouting won't help you. What code did you tried? If you don't show me your code. I simply can't help you – Steven Sep 19 '17 at 15:20