Android WebView - Youtube videos not playing within the application -
i have activity displaying web page using webview. within page, there link youtube video (so it's not video can or need embed).
the problem video won't play - when click on play button, error page appears saying " webpage not available webpage @ vnd.youtube:svf8ghl6d8xx might temporarily down , blah blah blah !!!"
please answer if know want! have gone through related posts on stackoverflow there no need of references other posts/question.
mainactivity.java
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); // add custom view action bar webview = (webview) findviewbyid(r.id.webview1); webview.getsettings().setjavascriptenabled(true); if(build.version.sdk_int < build.version_codes.jelly_bean_mr2) webview.getsettings().setpluginstate(websettings.pluginstate.on); //helps flash run on device webview.getsettings().setbuiltinzoomcontrols(true); webview.setwebviewclient(new webviewclient ()); webview.setwebchromeclient(new webchromeclient()); webview.getsettings().setusewideviewport(true); //webview.getsettings().setusewideviewport(false); webview.setscrollbarstyle(webview.scrollbars_outside_overlay); webview.loadurl(url); }
the following code make run in youtube app want videos run in app :(
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); // add custom view action bar webview = (webview) findviewbyid(r.id.webview1); webview.getsettings().setjavascriptenabled(true); webview.getsettings().setbuiltinzoomcontrols(true); if(build.version.sdk_int < build.version_codes.jelly_bean_mr2) webview.getsettings().setpluginstate(websettings.pluginstate.on); webview.setwebviewclient(new webviewclient () { @ override public boolean shouldoverrideurlloading(webview view, string url) { if (url.startswith("vnd.youtube")){ startactivity(new intent(intent.action_view, uri.parse(url))); return true; } else return false; } }); webview.setwebchromeclient(new webchromeclient()); webview.getsettings().setusewideviewport(true); //webview.getsettings().setusewideviewport(false); webview.setscrollbarstyle(webview.scrollbars_outside_overlay); webview.loadurl(url); }
go google developer console , select or create new project.
on left sidebar, select apis under apis & auth , turn status on youtube data api v3.
on left sidebar, select credentials , create new key under public api access.
when popup comes asking choose platform, select android key.
paste sha-1 key , project’s package name separated semicolon(;).
click on create. should see api key on dashboard.
download youtubeapi jar here
https://developers.google.com/youtube/android/player/downloads/
create layout this
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <com.google.android.youtube.player.youtubeplayerview android:id="@+id/youtube_player" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/black" android:layout_centerinparent="true" /> </relativelayout>
your activity should this
public class customyoutubeplayer extends youtubebaseactivity implements youtubeplayer.oninitializedlistener{ private string api_key="your key"; private string video_id; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); /** attaching layout xml **/ setcontentview(r.layout.youtube_player); /** initializing youtube player view **/ video_id = getintent().getextras().getstring(finalvariables.youtube_id); youtubeplayerview youtubeplayerview = (youtubeplayerview) findviewbyid(r.id.youtube_player); youtubeplayerview.initialize(api_key, this); } @override public void oninitializationsuccess(provider provider, youtubeplayer player, boolean wasrestored) { player.setshowfullscreenbutton(false); /** add listeners youtubeplayer instance **/ player.setplayerstatechangelistener(playerstatechangelistener); player.setplaybackeventlistener(playbackeventlistener); /** start buffering **/ if (!wasrestored) { player.loadvideo(video_id); } } private youtubeplayer.playbackeventlistener playbackeventlistener = new youtubeplayer.playbackeventlistener() { @override public void onbuffering(boolean arg0) { } @override public void onpaused() { } @override public void onplaying() { } @override public void onseekto(int arg0) { } @override public void onstopped() { } }; private youtubeplayer.playerstatechangelistener playerstatechangelistener = new youtubeplayer.playerstatechangelistener() { @override public void onadstarted() { } @override public void onerror(youtubeplayer.errorreason arg0) { } @override public void onloaded(string arg0) { } @override public void onloading() { } @override public void onvideoended() { finish(); } @override public void onvideostarted() { } }; }
just pass video id , that's simple that..
edit
if youtube link https://www.youtube.com/watch?v=rql_f8h3h9e video_id=rql_f8h3h9e extract video id youtube link , sent activity variable.
Comments
Post a Comment