Thursday, June 13, 2013

[android help] How to play video on a webview


How to play video on a webview



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


.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...