0

我用浏览器打开HTML文件,视频播放正常。但不能在 webview 中播放。以下是我的android代码和HTML代码。

安卓代码:

public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    WebChromeClient chromeClient = new WebChromeClient();
    String  url = new String("file:///android_asset/content0.html"); 
    WebView mWebView = (WebView) findViewById(R.id.webView);
    mWebView.setWebChromeClient(chromeClient);
    HelloWebViewClient hwc = new HelloWebViewClient();
    mWebView.setWebViewClient(hwc);
    mWebView.getSettings().setJavaScriptEnabled(true);
    hwc.shouldOverrideUrlLoading(mWebView,url);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;


}
class HelloWebViewClient extends WebViewClient { 
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
            if(url.indexOf(".3gp")!=-1 || url.indexOf(".mp4")!=-1 || url.indexOf(".flv")!=-1){
                    Intent intent=new Intent("android.intent.action.VIEW",Uri.parse(url));
                    view.getContext().startActivity(intent);
                    return true;
            }else{
            view.loadUrl(url); 
        return true; 
            }
    } 


}

}

html代码:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </head>
    <body  id="page">
        <div id="VideoBean" style="position:relative;" width="305" height="242">
            <video id="video" controls="controls" width="305" height="242"><source id="source" src="Sun_1-1.mp4">
            </video>
        </div>
    </body>
</html>

不知道哪里错了,但是肯定有问题,谁能告诉我?谢谢你!

4

0 回答 0