WebView shouldOverrideUrlLoading Wont Play Video

末鹿安然 提交于 2019-12-14 04:23:17

问题


My app displays a webpage using a WebView. I want the user to be able to click on a link to a video, and the video be played in landscape mode. Where I am now, the video doesn't even play??

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mnwv_main);     

myWebView = (WebView) findViewById(R.id.webview);
myWebView.getSettings().setJavaScriptEnabled(true); 
myWebView.getSettings().setPluginsEnabled(true);    

myWebView.setWebViewClient(new WebViewClient());      
myWebView.loadUrl("http://www.meanwhileinwv.com");

}     
public boolean shouldOverrideUrlLoading (WebView view, String url) {
if (url.endsWith(".mp4")){
    Intent in = new Intent (Intent.ACTION_VIEW , Uri.parse(url));
    startActivity(in);
    return true;
}
else
    return false;
}

回答1:


Change this:

...

myWebView.setWebViewClient(new WebViewClient());      
myWebView.loadUrl("http://www.meanwhileinwv.com");

}     
public boolean shouldOverrideUrlLoading (WebView view, String url) {
if (url.endsWith(".mp4")){
    Intent in = new Intent (Intent.ACTION_VIEW , Uri.parse(url));
    startActivity(in);
    return true;
}
else
    return false;
}

with this:

...

myWebView.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading (WebView view, String url) {
        if (url.endsWith(".mp4")){
            Intent in = new Intent (Intent.ACTION_VIEW , Uri.parse(url));
            startActivity(in);
            return true;
        }
        else
            return false;
        }

});      
myWebView.loadUrl("http://www.meanwhileinwv.com");

}     


来源:https://stackoverflow.com/questions/8258187/webview-shouldoverrideurlloading-wont-play-video

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