How to open video player in WebView?
I've a WebApp showed with Webview. In those pages I've some links to videos (MP4, 3GP...).
When I click on the link, nothing happens.?
public class luxgateway extends Activity {
WebView myWebView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myWebView = (WebView) findViewById(R.id.webview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setAllowFileAccess(true);
WebViewClient webViewClient = new MyWebViewClient();
myWebView.setWebViewClient(webViewClient);
myWebView.loadUrl("http://www.example.com/demo.html");
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
private class My开发者_运维技巧WebViewClient extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.endsWith(".3gp")) {
Intent intent = new Intent("android.intent.action.VIEW", Uri.parse(url));
view.getContext().startActivity(intent);
return true;
} else {
return super.shouldOverrideUrlLoading(view, url);
}
}
}
I've found some parts on this code on this server, but it doesn't help me"
if (url.endsWith(".3gp")) {
Intent intent = new Intent("android.intent.action.VIEW", Uri.parse(url));
view.getContext().startActivity(intent);
return true;
} else {
return super.shouldOverrideUrlLoading(view, url);
}
In this case I'm limited to 3GP files, but I'don't know which extension will be place on the WEBsite.
I've tried this code thisout the test, only to see if the video could be played (or videoplayer start with only this code
Intent intent = new Intent("android.intent.action.VIEW", Uri.parse(url));
view.getContext().startActivity(intent);
return true;
In this case, the video is only dowloaded. I want to have the sample interaction as the webrowser. When I click on the video link, the browser ask which player I want to user for this video.
You may have better luck if you specify that the uri contains video:
Intent intent = new Intent(Intent.ACTION_VIEW) //I encourage using this instead of specifying the string "android.intent.action.VIEW"
intent.setDataAndType(Uri.parse(url), "video/3gpp");
view.getContext().startActivity(intent);
Because we haven't specified a specific package to handle the above intent, it is called an implicit intent. For implicit intents, if there are multiple packages that handle the content type that you specify, you will be asked to choose between them. This should give you your desired behavior.
精彩评论