如何处理在android studio webview中点击.mht格式保存的音频链接?

时间:2017-09-19 13:41:36

标签: java android audio mhtml

我是android编程的初学者,我遇到了一个问题。我的布局中有一个webview,它加载了一个远程网页,我用“webView.saveWebArchive(file.getAbsolutePath());”保存了它。方法,这个页面有多个音频链接,点击它就会播放。但是当我使用mht格式保存并从本地存储器重新加载时,音频链接可以工作。我不知道如何处理它。 请帮帮我!!

这是我的代码:

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 个答案:

答案 0 :(得分:0)

查看此网站以查看支持的音频格式:https://developer.android.com/guide/topics/media/media-formats.html