mp4,3gp,mpeg,mp3在webview内部或外部流式传输

时间:2011-06-03 06:31:59

标签: android webview mp3 mp4 3gp

是否有通过webView运行mp4,3gp或mp3文件的确认解决方案?我终于让我的代码工作了

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

但行:

        return super.shouldOverrideUrlLoading

正在返回错误:“ 对于类型Activity“

,未定义方法shouldOverrideUrlLoading(WebView,String)

我无法在任何地方找到解决方案,我确信自己在构建这个项目方面做得很好,因为我过去6周才学习Android。

1 个答案:

答案 0 :(得分:1)

试试这个,

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

        }
    }