YouTube Android API: YouTubePlayerFragment loading spinner -
i using android youtube api samples create chromeless youtube player in app. having issue buffering / loading progress bar carries on displaying on video after has loaded , started playing. can reproduce in fragmentdemoactivity
sample couple of small modifications:
public class fragmentdemoactivity extends appcompatactivity implements youtubeplayer.oninitializedlistener { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.fragments_demo); youtubeplayerfragment youtubeplayerfragment = (youtubeplayerfragment) getfragmentmanager().findfragmentbyid(r.id.youtube_fragment); youtubeplayerfragment.initialize(developerkey.developer_key, this); } @override public void oninitializationsuccess(youtubeplayer.provider provider, youtubeplayer player, boolean wasrestored) { if (!wasrestored) { player.setplayerstyle(youtubeplayer.playerstyle.chromeless); player.loadvideo("ncgqdjiotg0", 10); } } @override public void oninitializationfailure(youtubeplayer.provider provider, youtubeinitializationresult youtubeinitializationresult) {} }
i have changed fragmentdemoactivity
inherit appcompatactivity
instead of youtubefailurerecoveryactivity
, documentation says fine do. have changed player style chromeless in oninitializationsuccess
. finally, have changed cuevideo
loadvideo
, trigger auto play.
this happens on multiple devices including nexus 5x. using library version 1.2.2. no error triggered in oninitializationfailure
.
the video starts playing after buffering. player chromeless. buffering spinner never disappears. bug, or doing not allowed do?
i encountered too, looks bug. here how managed work around it.
in oninitializationsuccess
, set playbackeventlistener
on player
. override onbuffering
, this:
viewgroup ytview = (viewgroup)ytplayerfragment.getview(); progressbar progressbar; try { // of 2016-02-16, progressbar @ position 0 -> 3 -> 2 in view tree of youtube player fragment viewgroup child1 = (viewgroup)ytview.getchildat(0); viewgroup child2 = (viewgroup)child1.getchildat(3); progressbar = (progressbar)child2.getchildat(2); } catch (throwable t) { // position may change, fallback looking progressbar = findprogressbar(ytview); // todo recommend reporting problem can update code in try branch: direct access more efficient searching } int visibility = isbuffering ? view.visible : view.invisible; if (progressbar != null) { progressbar.setvisibility(visibility); // note store progressbar instance somewhere here, , use later instead of accessing again. }
the findprogressbar
method, used fallback in case youtube code changes:
private progressbar findprogressbar(view view) { if (view instanceof progressbar) { return (progressbar)view; } else if (view instanceof viewgroup) { viewgroup viewgroup = (viewgroup)view; (int = 0; < viewgroup.getchildcount(); i++) { progressbar res = findprogressbar(viewgroup.getchildat(i)); if (res != null) return res; } } return null; }
this solution works fine me, enabling progressbar
when player buffering , disabling when it's not.
edit: if using solution discovers bug has been fixed or position of progressbar
has changed, please share can edit answer, thanks!
Comments
Post a Comment