Reputation: 13747
On bleacher report app they have a WebView with Youtube video in it. When you press the full screen icon the video than plays on full screen. How can I do that? Is it a server side issue or android one? I just want to add that even though it's a youtube video the player that you see is not the youtube player, its another player.
Edit: The youtube is a component is the url page. its not a youtube web page so I can not use "shouldOveriteUrl", also, I am not talking only about youtube videos, I am talking about any video component. Can I do something on my server like wrapping the video component with another player or something? Thanks!
Upvotes: 2
Views: 415
Reputation: 1782
// Try this this a sample Activity that will pay Youtube video
public class MyWebViewClient extends WebViewClient {
public Activity mActivity;
public MyWebViewClient(Activity activity,) {
super();
mActivity = activity;
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Uri uri = Uri.parse(url);
if (uri.getHost().contains("youtube.com")) {
IntentUtils.viewYoutube(mActivity, url);
return true;
}
return false;
}
public static void viewYoutube(Context context, String url) {
IntentUtils.viewWithPackageName(context, url, "com.google.android.youtube");
}
public static void viewWithPackageName(Context context, String url, String packageName) {
try {
Intent viewIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
if (isAppInstalled(context, packageName)) {
viewIntent.setPackage(packageName);
}
context.startActivity(viewIntent);
} catch (Exception e) {
Intent viewIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
context.startActivity(viewIntent);
}
}
public static boolean isAppInstalled(Context context, String packageName) {
PackageManager packageManager = context.getPackageManager();
try {
packageManager.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
return true;
} catch (NameNotFoundException e) {
}
return false;
}
@Override
public void onPageFinished(final WebView view, String url) {
String javascript = "javascript:" +
"var iframes = document.getElementsByTagName('iframe');" +
"for (var i = 0, l = iframes.length; i < l; i++) {" +
" var iframe = iframes[i]," +
" a = document.createElement('a');" +
" a.setAttribute('href', iframe.src);" +
" d = document.createElement('div');" +
" d.style.width = iframe.offsetWidth + 'px';" +
" d.style.height = iframe.offsetHeight + 'px';" +
" d.style.top = iframe.offsetTop + 'px';" +
" d.style.left = iframe.offsetLeft + 'px';" +
" d.style.position = 'absolute';" +
" d.style.opacity = '0';" +
" d.style.filter = 'alpha(opacity=0)';" +
" d.style.background = 'black';" +
" a.appendChild(d);" +
" iframe.offsetParent.appendChild(a);" +
"}";
view.loadUrl(javascript);
super.onPageFinished(view, url);
}
Upvotes: 1
Reputation: 165
If you want the video to be run on the webview without using the application with in the Android it must have the server side video player.
Webview will not by default allow Javascript which is most likely what your video player is using on the website. You would most likely need to tell your Webview to use Javascript in order for it to at least try to play the video (I say try because I have never done this myself, I usually use a player activity for video).
So by referencing your activies webview object you can do this:
browser = (WebView) findViewById(R.id.webbrowser_wvViewer);
browser.getSettings().setJavaScriptEnabled(true);
Hope this helps for you.. :)
Upvotes: 0