package Ġcz . spark o . course . android . youtube player Ċ Ċ import Ġandroid . content . Intent Ċ import Ġandroid . os . Bundle Ċ import Ġandroid . util . Log Ċ import Ġandroid . view . ViewGroup . LayoutParams . MATCH _ PARENT Ċ import Ġandroid . widget . Toast Ċ import Ġandroidx . constraint layout . widget . Constraint Layout Ċ import Ġcom . google . android . youtube . player . YouTube Base Activity Ċ import Ġcom . google . android . youtube . player . YouTube Initialization Result Ċ import Ġcom . google . android . youtube . player . YouTube Player Ċ import Ġcom . google . android . youtube . player . YouTube Player View Ċ Ċ const Ġval ĠY OUT UBE _ VIDEO _ ID Ġ= Ġ" 1 CR 0 Qm Ca MT s " Ċ const Ġval ĠY OUT UBE _ PLAY LIST _ ID Ġ= Ġ" PL N 2 6 Zv Uv Dx V TW B Go 0 w 4 IC W PU 6 - 7 BZ b 7 Kb " Ċ Ċ private Ġconst Ġval ĠTAG Ġ= Ġ" Y outube Activity " Ċ Ċ class ĠY outube Activity Ġ: ĠYouTube Base Activity (), ĠYouTube Player . On Initialized Listener Ġ{ ĊĠ Ġprivate Ġval ĠDIALOG _ REQUEST _ CODE Ġ= Ġ 1 ĊĊĠ Ġval Ġplayer View Ġby Ġlazy Ġ{ ĠYouTube Player View ( this ) Ġ} ĊĊĠ Ġoverride Ġfun ĠonCreate ( savedInstanceState : ĠBundle ?) Ġ{ ĊĠĠĠ Ġsuper . onCreate ( savedInstanceState ) ĊĠĠĠ Ġval Ġlayout Ġ= Ġlayout Inflater . inflate ( R . layout . activity _ youtube , Ġnull ) Ġas ĠConstraint Layout ĊĠĠĠ ĠsetContentView ( layout ) ĊĊĠĠĠ Ġplayer View . layout Params Ġ= ĠConstraint Layout . LayoutParams ( MATCH _ PARENT , ĠMATCH _ PARENT ) ĊĠĠĠ Ġlayout . addView ( player View ) ĊĊĠĠĠ Ġplayer View . initialize ( getString ( R . string . google _ api _ key ), Ġthis ) ĊĠ Ġ} ĊĊĠ Ġoverride Ġfun Ġon Initialization Success ( ĊĠĠĠ Ġprovider : ĠYouTube Player . Provider ?, ĊĠĠĠ Ġplayer : ĠYouTube Player ?, ĊĠĠĠ Ġwas Restore d : ĠBoolean ĊĠ Ġ) Ġ{ ĊĠĠĠ ĠLog . d ( TAG , Ġ" on Initialization Success : Ġprovider Ġis Ġ${ provider ?. java Class }") ĊĠĠĠ ĠLog . d ( TAG , Ġ" on Initialization Success : Ġplayer Ġis Ġ${ player ?. java Class }") ĊĠĠĠ ĠToast . makeText ( this , Ġ" Initialized ĠY outube Player Ġsuccessfully ", ĠToast . LENGTH _ SHORT ). show () ĊĊĠĠĠ Ġplayer ?. set Player State ChangeListener ( player State ChangeListener ) ĊĠĠĠ Ġplayer ?. set Playback EventListener ( playback EventListener ) ĊĊĠĠĠ Ġif Ġ(! was Restore d ) Ġ{ ĊĠĠĠĠĠ Ġplayer ?. load Video ( Y OUT UBE _ VIDEO _ ID ) ĊĠĠĠ Ġ} Ġelse Ġ{ ĊĠĠĠĠĠ Ġplayer ?. play () ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġoverride Ġfun Ġon Initialization Failure ( ĊĠĠĠ Ġprovider : ĠYouTube Player . Provider ?, ĊĠĠĠ Ġyou Tube Initialization Result : ĠYouTube Initialization Result ? ĊĠ Ġ) Ġ{ ĊĠĠĠ Ġif Ġ( you Tube Initialization Result ?. is User Recover able Error Ġ== Ġtrue ) Ġ{ ĊĠĠĠĠĠ Ġyou Tube Initialization Result . getError Dialog ( this , ĠDIALOG _ REQUEST _ CODE )?. show () ĊĠĠĠ Ġ} Ġelse Ġ{ ĊĠĠĠĠĠ ĠToast . makeText ( ĊĠĠĠĠĠĠĠ Ġthis , ĊĠĠĠĠĠĠĠ Ġ" Error Ġinitializing Ġthe ĠY outube Player Ġ[$ you Tube Initialization Result ]", ĊĠĠĠĠĠĠĠ ĠToast . LENGTH _ LONG ĊĠĠĠĠĠ Ġ). show () ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġoverride Ġfun Ġon Activity Result ( request Code : ĠInt , ĠresultCode : ĠInt , Ġdata : ĠIntent ?) Ġ{ ĊĠĠĠ ĠLog . d ( TAG , Ġ" on Activity Result : Ġrequest : Ġ[$ request Code ] Ġresult : Ġ[$ result Code ]") ĊĊĠĠĠ Ġif Ġ( request Code Ġ== ĠDIALOG _ REQUEST _ CODE ) Ġ{ ĊĠĠĠĠĠ ĠLog . d ( TAG , Ġ" on Activity Result : Ġ${ intent ?. toString () }") ĊĠĠĠĠĠ ĠLog . d ( TAG , Ġ" on Activity Result : Ġ${ intent ?. extras . toString () }") ĊĠĠĠĠĠ Ġplayer View . initialize ( getString ( R . string . google _ api _ key ), Ġthis ) ĊĠĠĠ Ġ} ĊĠ Ġ} ĊĊĠ Ġprivate Ġval Ġplayback EventListener Ġ= Ġobject : ĠYouTube Player . Playback EventListener Ġ{ ĊĠĠĠ Ġoverride Ġfun Ġon Playing () Ġ{ ĊĠĠĠĠĠ ĠToast . makeText ( this @ Y outube Activity , Ġ" Playback EventListener # on Playing ", ĠToast . LENGTH _ SHORT ). show () ĊĠĠĠĠĠ ĠLog . d ( TAG , Ġ" on Playing : Ġ") ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġon Stopped () Ġ{ ĊĠĠĠĠĠ ĠToast . makeText ( this @ Y outube Activity , Ġ" Playback EventListener # on Stopped ", ĠToast . LENGTH _ SHORT ). show () ĊĠĠĠĠĠ ĠLog . d ( TAG , Ġ" on Stopped : Ġ") ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġon Paused () Ġ{ ĊĠĠĠĠĠ ĠToast . makeText ( this @ Y outube Activity , Ġ" Playback EventListener # on Paused ", ĠToast . LENGTH _ SHORT ). show () ĊĠĠĠĠĠ ĠLog . d ( TAG , Ġ" on Paused : Ġ") ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġon Buffer ing ( p 0 : ĠBoolean ) Ġ{ ĊĠĠĠĠĠ ĠToast . makeText ( this @ Y outube Activity , Ġ" Playback EventListener # on Buffer ing ($ p 0 )", ĠToast . LENGTH _ SHORT ). show () ĊĠĠĠĠĠ ĠLog . d ( TAG , Ġ" on Buffer ing : Ġ$ p 0 ") ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġon Seek To ( p 0 : ĠInt ) Ġ{ ĊĠĠĠĠĠ ĠToast . makeText ( this @ Y outube Activity , Ġ" Playback EventListener # on Seek To ($ p 0 )", ĠToast . LENGTH _ SHORT ). show () ĊĠĠĠĠĠ ĠLog . d ( TAG , Ġ" on Seek To : Ġ$ p 0 ") ĊĠĠĠ Ġ} ĊĊĠ Ġ} ĊĊĠ Ġprivate Ġval Ġplayer State ChangeListener Ġ= Ġobject : ĠYouTube Player . Player State ChangeListener Ġ{ ĊĠĠĠ Ġoverride Ġfun Ġon Loading () Ġ{ ĊĠĠĠĠĠ ĠToast . makeText ( this @ Y outube Activity , Ġ" Player State ChangeListener # on Loading ", ĠToast . LENGTH _ SHORT ). show () ĊĠĠĠĠĠ ĠLog . d ( TAG , Ġ" on Loading : Ġ") ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġon Loaded ( p 0 : ĠString ?) Ġ{ ĊĠĠĠĠĠ ĠToast . makeText ( this @ Y outube Activity , Ġ" Player State ChangeListener # on Loaded ($ p 0 )", ĠToast . LENGTH _ SHORT ). show () ĊĠĠĠĠĠ ĠLog . d ( TAG , Ġ" on Loaded : Ġ$ p 0 ") ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġon Ad Started () Ġ{ ĊĠĠĠĠĠ ĠToast . makeText ( this @ Y outube Activity , Ġ" Player State ChangeListener # on Ad Started ", ĠToast . LENGTH _ SHORT ). show () ĊĠĠĠĠĠ ĠLog . d ( TAG , Ġ" on Ad Started : Ġ") ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġon Video Started () Ġ{ ĊĠĠĠĠĠ ĠToast . makeText ( this @ Y outube Activity , Ġ" Player State ChangeListener # on Video Started ", ĠToast . LENGTH _ SHORT ). show () ĊĠĠĠĠĠ ĠLog . d ( TAG , Ġ" on Video Started : Ġ") ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġon Video Ended () Ġ{ ĊĠĠĠĠĠ ĠToast . makeText ( this @ Y outube Activity , Ġ" Player State ChangeListener # on Video Ended ", ĠToast . LENGTH _ SHORT ). show () ĊĠĠĠĠĠ ĠLog . d ( TAG , Ġ" on Video Ended : Ġ") ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun ĠonError ( p 0 : ĠYouTube Player . Error Reason ?) Ġ{ ĊĠĠĠĠĠ ĠToast . makeText ( this @ Y outube Activity , Ġ" Player State ChangeListener # onError ($ p 0 )", ĠToast . LENGTH _ SHORT ). show () ĊĠĠĠĠĠ ĠLog . d ( TAG , Ġ" onError : Ġ$ p 0 ") ĊĠĠĠ Ġ} ĊĠ Ġ} Ċ } Ċ