android - Youtube Video 400 network Error when try to play video -
i try play video using youtube video api
when put static id
means declare id in file @ time code working.
but when try fetch id getintent()
got id video not play.
i got there problem network.
below code. working in condition.
this video_activity.java
public class video_activity extends appcompatactivity implements youtubeplayer.oninitializedlistener { textview textview1, textview2; scrollview scrollview; webview webview; videoview videoview; private mediacontroller mcontroller; private uri uriyoutube; private youtubeplayerview youtubeview; private static final int recovery_dialog_request = 1; video video; public string videourl; // google console apis developer key // replace key your's string youtube_video_code; string developer_key; //string video_ids; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_video_); developer_key = "aizasyc0rjdlc0jkyy0z66qgonknmkoayoqpqni"; // youtube video id youtube_video_code = "yexsciwjfte"; getsupportactionbar().settitle(html.fromhtml("<font color='#fdfafa'> video view </font>")); final drawable uparrow = getresources().getdrawable(r.drawable.abc_ic_ab_back_mtrl_am_alpha); getsupportactionbar().sethomeasupindicator(uparrow); getsupportactionbar().setnavigationmode(actionbar.navigation_mode_standard); getsupportactionbar().setdisplayhomeasupenabled(true); youtubeplayersupportfragment frag = (youtubeplayersupportfragment) getsupportfragmentmanager().findfragmentbyid(r.id.youtube_view); if (frag == null) { frag = youtubeplayersupportfragment.newinstance(); getsupportfragmentmanager().begintransaction().add(r.id.youtube_view, frag).commit(); } frag.initialize(developer_key, this); intent intent = getintent(); if (null != intent) { string video_title = intent.getstringextra("video_title"); string video_desc = intent.getstringextra("vide_desc"); //video_ids = intent.getstringextra("video_id"); //log.i("video_url", " " + youtube_videoplayer); textview1 = (textview) findviewbyid(r.id.activity_video_title); textview1.settext(video_title); textview2 = (textview) findviewbyid(r.id.activity_video_desc); textview2.settext(video_desc); scrollview = (scrollview) findviewbyid(r.id.scroller_id_video); scrollview.post(new runnable() { @override public void run() { scrollview.smoothscrollto(0, textview2.getbottom()); } }); } } @override public void oninitializationfailure (youtubeplayer.provider provider,youtubeinitializationresult errorreason){ if (errorreason.isuserrecoverableerror()) { errorreason.geterrordialog(this, recovery_dialog_request).show(); } else { //string youtube_videplayer = getintent().getstringextra("video_player"); string youtube_videoplayer = string.format(getstring(r.string.error_player), errorreason.tostring()); toast.maketext(this, youtube_videoplayer, toast.length_long).show(); } } @override public void oninitializationsuccess (youtubeplayer.provider provider,youtubeplayer player,boolean wasrestored){ if (!wasrestored) { // loadvideo() auto play video // use cuevideo() method, if don't want play automatically player.cuevideo(youtube_video_code); //player.cuevideo(video_ids); // hiding player controls //player.setplayerstyle(youtubeplayer.playerstyle.chromeless); } } @override protected void onactivityresult ( int requestcode, int resultcode, intent data){ if (requestcode == recovery_dialog_request) { // retry initialization if user performed recovery action getyoutubeplayerprovider().initialize(video_config.developer_key, this); } } private youtubeplayer.provider getyoutubeplayerprovider() { return (youtubeplayerview) findviewbyid(r.id.youtube_view); } @override public boolean onoptionsitemselected(menuitem item) { if(item.getitemid() == android.r.id.home){ finish(); return true; } return super.onoptionsitemselected(item); }
}
now when used
public class video_activity extends appcompatactivity implements youtubeplayer.oninitializedlistener { textview textview1, textview2; scrollview scrollview; webview webview; videoview videoview; private mediacontroller mcontroller; private uri uriyoutube; private youtubeplayerview youtubeview; private static final int recovery_dialog_request = 1; video video; public string videourl; // google console apis developer key // replace key your's public static final string developer_key = "aizasyc0rjdlc0jkyy0z66qgonknmkoayoqpqni"; // youtube video id //public static final string youtube_video_code = "yexsciwjfte"; string video_ids; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_video_); getsupportactionbar().settitle(html.fromhtml("<font color='#fdfafa'> video view </font>")); final drawable uparrow = getresources().getdrawable(r.drawable.abc_ic_ab_back_mtrl_am_alpha); getsupportactionbar().sethomeasupindicator(uparrow); getsupportactionbar().setnavigationmode(actionbar.navigation_mode_standard); getsupportactionbar().setdisplayhomeasupenabled(true); youtubeplayersupportfragment frag = (youtubeplayersupportfragment) getsupportfragmentmanager().findfragmentbyid(r.id.youtube_view); if (frag == null) { frag = youtubeplayersupportfragment.newinstance(); getsupportfragmentmanager().begintransaction().add(r.id.youtube_view, frag).commit(); } frag.initialize(developer_key, this); intent intent = getintent(); if (null != intent) { string video_title = intent.getstringextra("video_title"); string video_desc = intent.getstringextra("vide_desc"); video_ids = intent.getstringextra("video_id"); //log.i("video_url", " " + youtube_videoplayer); textview1 = (textview) findviewbyid(r.id.activity_video_title); textview1.settext(video_title); textview2 = (textview) findviewbyid(r.id.activity_video_desc); textview2.settext(video_desc); scrollview = (scrollview) findviewbyid(r.id.scroller_id_video); scrollview.post(new runnable() { @override public void run() { scrollview.smoothscrollto(0, textview2.getbottom()); } }); } } @override public void oninitializationfailure (youtubeplayer.provider provider,youtubeinitializationresult errorreason){ if (errorreason.isuserrecoverableerror()) { errorreason.geterrordialog(this, recovery_dialog_request).show(); } else { //string youtube_videplayer = getintent().getstringextra("video_player"); string youtube_videoplayer = string.format(getstring(r.string.error_player), errorreason.tostring()); toast.maketext(this, youtube_videoplayer, toast.length_long).show(); } } @override public void oninitializationsuccess (youtubeplayer.provider provider,youtubeplayer player,boolean wasrestored){ if (!wasrestored) { // loadvideo() auto play video // use cuevideo() method, if don't want play automatically //player.cuevideo(youtube_videoplayer); player.cuevideo(video_ids); // hiding player controls //player.setplayerstyle(youtubeplayer.playerstyle.chromeless); } } @override protected void onactivityresult ( int requestcode, int resultcode, intent data){ if (requestcode == recovery_dialog_request) { // retry initialization if user performed recovery action getyoutubeplayerprovider().initialize(video_config.developer_key, this); } } private youtubeplayer.provider getyoutubeplayerprovider() { return (youtubeplayerview) findviewbyid(r.id.youtube_view); } @override public boolean onoptionsitemselected(menuitem item) { if(item.getitemid() == android.r.id.home){ finish(); return true; } return super.onoptionsitemselected(item); }
}
youtube id extraction different question since can solve problem answering this. , it's working.
public void string getyoutubeid(string yturl) { string youtubeid =""; if (yturl.contains("://youtu.be/")){ youtubeid=yturl.substring(yturl.lastindexof("/") + 1); yturl="http://youtube.com/watch?v=" + youtubeid; }else if (yturl.contains("watch?v=")){ if (yturl.contains("&")){ yturl=yturl.substring(0, yturl.indexof('&')); } if (yturl.contains("https")){ yturl.replace("https", "http"); } youtubeid=yturl.substring(yturl.indexof("watch?v=") + 8); } return youtubeid; }
call getyoutubeid(youtube url)
youtube id. print id , compare url passing , see if both matches.
Comments
Post a Comment