mp4, 3gp, mpeg, mp3 streamed inside or outside of webview

*爱你&永不变心* 提交于 2019-12-24 21:42:22

问题


Is there any confirmed solution to running mp4, 3gp or mp3 files via webView? I finally have my code working

public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if (url.endsWith(".mp3")) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.parse(url), "audio/*");
        view.getContext().startActivity(intent);   
        return true;
    } else if (url.endsWith(".mp4") || url.endsWith(".3gp")) {
            Intent intent = new Intent(Intent.ACTION_VIEW); 
            intent.setDataAndType(Uri.parse(url), "video/*");
            view.getContext().startActivity(intent);   
            return true;
    } else {
        return super.shouldOverrideUrlLoading(view, url);
    }
}

however the line:

        return super.shouldOverrideUrlLoading

is returning the error: " The method shouldOverrideUrlLoading(WebView, String) is undefined for the type Activity"

I simply cannot find a solution for this anywhere and I'm sure I've done a great job building this project given that I've only been learning Android for the past 6 weeks.


回答1:


try this,

@Override
    protected void onCreate(Bundle savedInstanceState) {        

        super.onCreate(savedInstanceState);

        setContentView(R.layout.articlelayout);

        mWebView = (WebView) findViewById(R.id.webview);


             mWebView.getSettings().setJavaScriptEnabled(true);        
             mWebView.getSettings().setAllowFileAccess(true);
             mWebView.getSettings().setPluginsEnabled(true);
             mWebView.setWebViewClient(new ArticleWebViewClient());                

    }




private class ArticleWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.endsWith(".mp3")) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setDataAndType(Uri.parse(url), "audio/*");
                startActivity(intent);

                return true;

            } else if (url.endsWith(".mp4") || url.endsWith(".3gp")) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setDataAndType(Uri.parse(url), "video/*");
                startActivity(intent);
                return true;
            } else {
                return false;
            }
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);

        }
    }


来源:https://stackoverflow.com/questions/6223744/mp4-3gp-mpeg-mp3-streamed-inside-or-outside-of-webview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!