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

2019-08-28 05:59发布

问题:

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);

        }
    }