android - how can I call a YouTube intent from this code? -


how can change android code call youtube intent? want make fire off youtube player inside app. think player being called not compatible youtube streaming videos.

here code snippet below:

@override         protected void onpostexecute(detailsoverviewrow row) {             /* 1st row: detailsoverviewrow */                /* action setting*/             sparsearrayobjectadapter sparsearrayobjectadapter = new sparsearrayobjectadapter();             sparsearrayobjectadapter.set(0, new action(action_play_video, "play video"));             sparsearrayobjectadapter.set(1, new action(1, "action 2", "label"));             sparsearrayobjectadapter.set(2, new action(2, "action 3", "label"));              row.setactionsadapter(sparsearrayobjectadapter);              mfwdorpresenter.setonactionclickedlistener(new onactionclickedlistener() {                 @override                 public void onactionclicked(action action) {                     if (action.getid() == action_play_video) {                         intent intent = new intent(getactivity(), playbackoverlayactivity.class);                         intent.putextra(getresources().getstring(r.string.movie), mselectedmovie);                         intent.putextra(getresources().getstring(r.string.should_start), true);                         startactivity(intent);                     }                 }             }); 

update: here playeroverlay activity code:

public class playbackoverlayactivity extends activity {      private static final string tag = playbackoverlayactivity.class.getsimplename();      private videoview mvideoview;      private leanbackplaybackstate mplaybackstate = leanbackplaybackstate.idle;      private int mposition = 0;     private long mstarttimemillis;     private long mduration = -1;      /*      * list of various states can in      */     public enum leanbackplaybackstate {         playing, paused, idle     }      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_playback_overlay);          loadviews();     }      @override     public void ondestroy() {         super.ondestroy();         stopplayback();         mvideoview.suspend();         mvideoview.setvideouri(null);     }      private void loadviews() {         mvideoview = (videoview) findviewbyid(r.id.videoview);         mvideoview.setfocusable(false);         mvideoview.setfocusableintouchmode(false);          movie movie = (movie) getintent().getserializableextra(detailsactivity.movie);         setvideopath(movie.getvideourl());      }      public void setvideopath(string videourl) {         setposition(0);         mvideoview.setvideopath(videourl);         mstarttimemillis = 0;         mduration = utils.getduration(videourl);     }      private void stopplayback() {         if (mvideoview != null) {             mvideoview.stopplayback();         }     }      private void setposition(int position) {         if (position > mduration) {             mposition = (int) mduration;         } else if (position < 0) {             mposition = 0;             mstarttimemillis = system.currenttimemillis();         } else {             mposition = position;         }         mstarttimemillis = system.currenttimemillis();         log.d(tag, "position set " + mposition);     }      public int getposition() {         return mposition;     }      public void setplaybackstate(leanbackplaybackstate playbackstate) {         this.mplaybackstate = playbackstate;     }      @override     public boolean oncreateoptionsmenu(menu menu) {         // inflate menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.menu_playback_overlay, menu);         return true;     }      @override     public boolean onoptionsitemselected(menuitem item) {         // handle action bar item clicks here. action bar         // automatically handle clicks on home/up button, long         // specify parent activity in androidmanifest.xml.         int id = item.getitemid();          //noinspection simplifiableifstatement         if (id == r.id.action_settings) {             return true;         }          return super.onoptionsitemselected(item);     }      public void playpause(boolean doplay) {         if (mplaybackstate == leanbackplaybackstate.idle) {             /* callbacks mvideoview */             setupcallbacks();         }          if (doplay && mplaybackstate != leanbackplaybackstate.playing) {             mplaybackstate = leanbackplaybackstate.playing;             if (mposition > 0) {                 mvideoview.seekto(mposition);             }             mvideoview.start();             mstarttimemillis = system.currenttimemillis();         } else {             mplaybackstate = leanbackplaybackstate.paused;             int timeelapsedsincestart = (int) (system.currenttimemillis() - mstarttimemillis);             setposition(mposition + timeelapsedsincestart);             mvideoview.pause();         }     }      public void fastforward() {         if (mduration != -1) {             // fast forward 10 seconds.             setposition(mvideoview.getcurrentposition() + (10 * 1000));             mvideoview.seekto(mposition);         }     }      public void rewind() {         // rewind 10 seconds         setposition(mvideoview.getcurrentposition() - (10 * 1000));         mvideoview.seekto(mposition);     }      private void setupcallbacks() {          mvideoview.setonerrorlistener(new mediaplayer.onerrorlistener() {              @override             public boolean onerror(mediaplayer mp, int what, int extra) {                 mvideoview.stopplayback();                 mplaybackstate = leanbackplaybackstate.idle;                 return false;             }         });          mvideoview.setonpreparedlistener(new mediaplayer.onpreparedlistener() {             @override             public void onprepared(mediaplayer mp) {                 if (mplaybackstate == leanbackplaybackstate.playing) {                     mvideoview.start();                 }             }         });          mvideoview.setoncompletionlistener(new mediaplayer.oncompletionlistener() {             @override             public void oncompletion(mediaplayer mp) {                 mplaybackstate = leanbackplaybackstate.idle;             }         });     } } 

thanks!

ironmantis7x

use youtube api android

-download youtube api client

-the instructions registering application explain how register app in google developers console , obtain android api key, need use api.

-an example of code

public class playervideo extends youtubebaseactivity implements youtubeplayer.oninitializedlistener {  private static final int recovery_dialog_request = 1; public boolean flag_full=false;   public string dev_key= "your devlopper key";       int key; private youtubeplayerview youtubeplayerview;   @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_trailler_play);     bundle extras=getintent().getextras();     if(extras!=null){          key=extras.getint("id");      }      youtubeplayerview=(youtubeplayerview)findviewbyid(r.id.youtube_view);        youtubeplayerview.initialize(config.developer_key,this);     youtubeplayerview.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {             flag_full=true;          }     });  }     @override public void oninitializationsuccess(youtubeplayer.provider provider, youtubeplayer player, boolean wasrestored) {     if (!wasrestored) {         player.setfullscreen(flag_full);         // loadvideo() auto play video         // use cuevideo() method, if don't want play automatically          player.loadvideo(getintent().getstringextra("id"));           // hiding player controls         player.setplayerstyle(youtubeplayer.playerstyle.default);     }     //player.cuevideo(getintent().getstringextra("id")); }  @override public void oninitializationfailure(youtubeplayer.provider provider, youtubeinitializationresult errorreason) {     if (errorreason.isuserrecoverableerror()) {         errorreason.geterrordialog(this, recovery_dialog_request).show();     } else {         string errormessage = string.format(                 getstring(r.string.error_player), errorreason.tostring());         toast.maketext(this, errormessage, toast.length_long).show();     } }  @override protected void onactivityresult(int requestcode, int resultcode, intent data) {     if(requestcode==recovery_dialog_request){         getyoutubeplayerprovider().initialize(config.developer_key, this);     } } private youtubeplayer.provider getyoutubeplayerprovider() {     return (youtubeplayerview) findviewbyid(r.id.youtube_view); } 

}

and example of intent

intent i=new intent(getactivity().getapplication(), traillerplay.class);                     i.putextra("id", "youtube_video_id");                     startactivity(i); 

Comments