regex - How to get youtube video id from URL with java -
i try id
of youtube video url. here's example of such url:
https://www.youtube.com/watch?v=iz6ar-watyy
how can id of video?
this video_adapter
public class videoadapter extends recyclerview.adapter<videoadapter.myviewholder> { private list<video> videolist; private context context; string videourl; intent intent; public class myviewholder extends recyclerview.viewholder { public textview title; public imageview image, videonextarrowimage; public textview desc; public textview videourl; public myviewholder(view view) { super(view); title = (textview) view.findviewbyid(r.id.video_title); image = (imageview) view.findviewbyid(r.id.video_imageview); desc = (textview) view.findviewbyid(r.id.video_desc); videourl = (textview) view.findviewbyid(r.id.video_url); videonextarrowimage = (imageview) view.findviewbyid(r.id.video_nextarrow); view.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent = new intent(context, video_activity.class); context.startactivity(i); } }); } } public videoadapter(list<video> videolist, context context) { this.videolist = videolist; this.context = context; } @override public myviewholder oncreateviewholder(viewgroup parent, int viewtype) { view itemview = layoutinflater.from(parent.getcontext()).inflate(r.layout.video_list_row, parent, false); return new myviewholder(itemview); } @override public void onbindviewholder(myviewholder holder, final int position) { final video video = videolist.get(position); holder.title.settext(video.gettitle()); holder.videourl.settext(video.getvideourl()); //holder.image.setimageresource(r.drawable.more_selector); holder.desc.settext(video.getdesc()); holder.videonextarrowimage.setimageresource(r.drawable.nextbutton); context = holder.image.getcontext(); picasso.with(context).load("http://bitstobyte.in/upload/" + video.getimage()).placeholder(r.drawable.play).error(r.drawable.startyoutubevideo).resize(50, 50).into(holder.image); holder.title.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { string title = video.gettitle(); string desc = video.getdesc(); string[] links = new string[]{video.getvideourl()}; string[] ids = new string[links.length]; (int = 0; < links.length; i++) { ids[i] = links[i].split("v=")[1]; } intent = new intent(context, video_activity.class); intent.putextra("video_title", title); intent.putextra("vide_desc", desc); intent.putextra("video_id", ids); context.startactivity(intent); toast.maketext(context, "you clicked " + video.gettitle(), toast.length_long).show(); } }); } @override public int getitemcount() { return videolist.size(); }
}
this video.java
public class video extends videoresponse{ public string title; public string image; public string desc; private string videourl; public video(){ } public video(string title, string image, string desc,string videourl) { this.title = title; this.image = image; this.videourl = videourl; this.desc = desc; } public string gettitle() { return title; } public void settitle(string name) { this.title = name; } public string getimage() { return image; } public void setimage(string image) { this.image = image; } public string getdesc() { return desc; } public void setdesc(string desc) { this.desc = desc; } public string getvideourl() { return videourl; } public void setvideourl(string videourl) { this.videourl = videourl; }
}
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 public static final string developer_key = "aizasyc0rjdlc0jkyy0z66qgonknmkoayoqpqni"; // youtube video id //public static final string youtube_video_code = "yexsciwjfte"; string[] 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"); ids = intent.getstringarrayextra("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(string.valueof(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); }
}
you can use string.split(regex)
for example:
string s = "https://www.youtube.com/watch?v=iz6ar-watyy"; string id = s.split("v=")[1];
Comments
Post a Comment