android - YouTube Video not playing in WebView -
youtube video not playing in webview.
it first app ,i want webview. when open youtube video not playing, loading not playing. loading time. thank helping.
java:
public class mainactivity extends activity { private webview mwebview; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mwebview = (webview) findviewbyid(r.id.webview); mwebview.getsettings().setjavascriptenabled(true); mwebview.loadurl("http://www.google.com"); mwebview.setwebviewclient(new hellowebviewclient()); } private class hellowebviewclient extends webviewclient { @override public boolean shouldoverrideurlloading(webview webview, string url) { webview.loadurl(url); return true; }} @override public boolean onkeydown(int keycode, keyevent event) { if ((keycode == keyevent.keycode_back) && mwebview.cangoback()) { mwebview.goback(); return true; } return super.onkeydown(keycode, event); } }
xml:
<?xml version="1.0" encoding="utf-8"?> <webview xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/webview" android:layout_width="fill_parent" android:layout_height="fill_parent" />
seems duplicate of play youtube video in webview , youtube video not playing in webview - android
to make work via webview, used following 2 steps (version 4.2.2/nexus 4):
on
shouldoverrideurlloading
addedwebview.setwebchromeclient(new webchromeclient());
in
androidmanifest.xml
activity
tag addedandroid:hardwareaccelerated="true"
androidmanifest.xml should contain internet permission.
while exact steps playing video via webview can specific device , android version, there other alternatives webview, videoview or using youtube android player api.
edit1
as pause , full screen, second link mentioned in beginning. make easier, posting code worked on nexus.
in activity_main.xml
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".mainactivity" > <framelayout android:id="@+id/fullscreen_custom_content" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ff000000"/> <linearlayout android:id="@+id/linearlayout" android:layout_width="fill_parent" android:layout_height="fill_parent"> <webview android:id="@+id/webview" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </linearlayout> </relativelayout>
in mainactivity class:
public class mainactivity extends activity { private webview mwebview; private linearlayout mcontentview; private framelayout mcustomviewcontainer; private webchromeclient.customviewcallback mcustomviewcallback; framelayout.layoutparams cover_screen_gravity_center = new framelayout.layoutparams( viewgroup.layoutparams.wrap_content, viewgroup.layoutparams.wrap_content, gravity.center); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mcontentview = (linearlayout) findviewbyid(r.id.linearlayout); mwebview = (webview) findviewbyid(r.id.webview); mcustomviewcontainer = (framelayout) findviewbyid(r.id.fullscreen_custom_content); websettings websettings = mwebview.getsettings(); websettings.setpluginstate(websettings.pluginstate.on); websettings.setjavascriptenabled(true); websettings.setusewideviewport(true); websettings.setloadwithoverviewmode(true); mwebview.loadurl("http://www.google.com"); mwebview.setwebviewclient(new hellowebviewclient()); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } private class hellowebviewclient extends webviewclient { @override public boolean shouldoverrideurlloading(webview webview, string url) { webview.setwebchromeclient(new webchromeclient() { private view mcustomview; @override public void onshowcustomview(view view, webchromeclient.customviewcallback callback) { // if view exists terminate new 1 if (mcustomview != null) { callback.oncustomviewhidden(); return; } // add custom view container. mcustomviewcontainer.addview(view, cover_screen_gravity_center); mcustomview = view; mcustomviewcallback = callback; // hide main browser view mcontentview.setvisibility(view.gone); // show custom view container. mcustomviewcontainer.setvisibility(view.visible); mcustomviewcontainer.bringtofront(); } }); webview.loadurl(url); return true; } } @override public boolean onkeydown(int keycode, keyevent event) { if ((keycode == keyevent.keycode_back) && mwebview.cangoback()) { mwebview.goback(); return true; } return super.onkeydown(keycode, event); } }
edit2 - adding button support
public class mainactivity extends activity { private webview mwebview; private linearlayout mcontentview; private framelayout mcustomviewcontainer; private view mcustomview; private webchromeclient.customviewcallback mcustomviewcallback; framelayout.layoutparams cover_screen_gravity_center = new framelayout.layoutparams( viewgroup.layoutparams.wrap_content, viewgroup.layoutparams.wrap_content, gravity.center); private webchromeclient mwebchromeclient; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mcontentview = (linearlayout) findviewbyid(r.id.linearlayout); mwebview = (webview) findviewbyid(r.id.webview); mcustomviewcontainer = (framelayout) findviewbyid(r.id.fullscreen_custom_content); mwebchromeclient = new webchromeclient() { @override public void onshowcustomview(view view, webchromeclient.customviewcallback callback) { // if view exists terminate new 1 if (mcustomview != null) { callback.oncustomviewhidden(); return; } // add custom view container. mcustomviewcontainer.addview(view, cover_screen_gravity_center); mcustomview = view; mcustomviewcallback = callback; // hide main browser view mcontentview.setvisibility(view.gone); // show custom view container. mcustomviewcontainer.setvisibility(view.visible); mcustomviewcontainer.bringtofront(); } @override public void onhidecustomview() { if (mcustomview == null) return; // hide custom view. mcustomview.setvisibility(view.gone); // remove custom view container. mcustomviewcontainer.removeview(mcustomview); mcustomview = null; mcustomviewcontainer.setvisibility(view.gone); mcustomviewcallback.oncustomviewhidden(); // show content view. mcontentview.setvisibility(view.visible); } }; websettings websettings = mwebview.getsettings(); websettings.setpluginstate(websettings.pluginstate.on); websettings.setjavascriptenabled(true); websettings.setusewideviewport(true); websettings.setloadwithoverviewmode(true); mwebview.loadurl("http://www.google.com"); mwebview.setwebviewclient(new hellowebviewclient()); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } private class hellowebviewclient extends webviewclient { @override public boolean shouldoverrideurlloading(webview webview, string url) { webview.setwebchromeclient(mwebchromeclient); webview.loadurl(url); return true; } } @override protected void onstop() { super.onstop(); if (mcustomview != null) { if (mcustomviewcallback != null) mcustomviewcallback.oncustomviewhidden(); mcustomview = null; } } @override public void onbackpressed() { super.onbackpressed(); if (mcustomview != null) { mwebchromeclient.onhidecustomview(); } else { finish(); } } @override public boolean onkeydown(int keycode, keyevent event) { if ((keycode == keyevent.keycode_back) && mwebview.cangoback()) { mwebview.goback(); return true; } return super.onkeydown(keycode, event); } }
Comments
Post a Comment