android scrollview - Play/Pause video when listitem get visible in recyclerview -
i have implemented recyclerview in adding textures views list items play videos url. vine , instagram app want play video when visible in recyclerview , stop/pause video when listitem gets out of screen. following code:
videosadapter class:
public class videosadapter extends recyclerview.adapter<videosadapter.viewholder> { private static string tag = "videosadapter"; context context; private arraylist<string> urls; recyclerview recyclerview; public static class viewholder extends recyclerview.viewholder { public textureview textureview; public textview textview; public viewholder(view v) { super(v); textureview = (textureview) v.findviewbyid(r.id.textureview); textview = (textview) v.findviewbyid(r.id.textview); } } public videosadapter(context context, recyclerview recyclerview, final arraylist<string> urls) { this.context = context; this.recyclerview = recyclerview; this.urls = urls; recyclerview.addonscrolllistener(new recyclerview.onscrolllistener() { @override public void onscrolled(recyclerview recyclerview, int dx, int dy) { super.onscrolled(recyclerview, dx, dy); } @override public void onscrollstatechanged(recyclerview recyclerview, int newstate) { super.onscrollstatechanged(recyclerview, newstate); if(newstate == recyclerview.scroll_state_idle) { linearlayoutmanager layoutmanager = ((linearlayoutmanager) recyclerview.getlayoutmanager()); int firstvisibleposition = layoutmanager.findfirstvisibleitemposition(); int findfirstcompletelyvisibleitemposition = layoutmanager.findfirstcompletelyvisibleitemposition(); int findlastvisibleitemposition = layoutmanager.findlastvisibleitemposition(); int findlastcompletelyvisibleitemposition = layoutmanager.findlastcompletelyvisibleitemposition(); *//*log.i(tag, "firstvisibleposition = " + string.valueof(firstvisibleposition)); log.i(tag, "findfirstcompletelyvisibleitemposition = " + string.valueof(findfirstcompletelyvisibleitemposition)); log.i(tag, "findlastvisibleitemposition = " + string.valueof(findlastvisibleitemposition)); log.i(tag, "findlastcompletelyvisibleitemposition = " + string.valueof(findlastcompletelyvisibleitemposition));*//* log.i(tag, "//////////////////////////////////////////////////////////////"); if(findfirstcompletelyvisibleitemposition>=0) { log.i(tag, "playing_url = " + urls.get(findfirstcompletelyvisibleitemposition)); int temppreviousurl = findfirstcompletelyvisibleitemposition - 1; if(temppreviousurl>=0) log.i(tag, "stop_playing_url = " + urls.get(temppreviousurl)); } else { log.i(tag, "playing_url = " + urls.get(firstvisibleposition)); int temppreviousurl = firstvisibleposition - 1; if(temppreviousurl>=0) log.i(tag, "stop_playing_url = " + urls.get(temppreviousurl)); } } } }); } // create new views (invoked layout manager) @override public videosadapter.viewholder oncreateviewholder(viewgroup parent, int viewtype) { // create new view view v = layoutinflater.from(parent.getcontext()).inflate(r.layout.view_main, parent, false); viewholder viewholder = new viewholder(v); return viewholder; } // replace contents of view (invoked layout manager) @override public void onbindviewholder(viewholder holder, int position) { string url = urls.get(position); holder.textview.settext(url); //log.i("bindviewholdercalledfor", position + " = " + url); videoplaycontroller videoplaycontroller = new videoplaycontroller(context, recyclerview, holder.textureview, url); if(position==0) { videoplaycontroller.loadvideo(); } } @override public int getitemcount() { return urls.size(); }
}
videoplaycontroller class:
public class videoplaycontroller implements textureview.surfacetexturelistener { private static string tag = "videoplaycontroller"; context context; string url; mediaplayer mp; surface surface; surfacetexture s; recyclerview recyclerview; textureview textureview; public videoplaycontroller(context context, recyclerview recyclerview, textureview textureview, final string url) { this.context = context; this.recyclerview = recyclerview; this.textureview = textureview; this.url = url; recyclerview.addonscrolllistener(new recyclerview.onscrolllistener() { @override public void onscrollstatechanged(recyclerview recyclerview, int newstate) { super.onscrollstatechanged(recyclerview, newstate); if(newstate == recyclerview.scroll_state_idle) { log.i(tag, "onscrollstatechangedcalled for: " + url); //when scroll @ idol position check if item visible } } }); } public void loadvideo() { textureview.setsurfacetexturelistener(this); } @override public void onsurfacetextureavailable(final surfacetexture surface, int width, int height) { log.d("surface_available-url", this.url); startvideo(surface); } @override public void onsurfacetexturesizechanged(surfacetexture surface, int width, int height) { log.d("surface_destroyed-url", this.url); } @override public boolean onsurfacetexturedestroyed(surfacetexture surface) { log.d("surface_destroyed-url", this.url); this.mp.stop(); this.mp.reset(); this.mp.release(); this.mp = null; return false; } @override public void onsurfacetextureupdated(surfacetexture surface) { } public void startvideo(surfacetexture t) { this.surface = new surface(t); this.mp = new mediaplayer(); this.mp.setsurface(this.surface); try { uri uri = uri.parse(this.url); this.mp.setdatasource(url); this.mp.prepareasync(); this.mp.setonpreparedlistener(new mediaplayer.onpreparedlistener() { public void onprepared(mediaplayer mp) { mp.setlooping(true); mp.start(); } }); } catch (illegalargumentexception e1) { e1.printstacktrace(); } catch (securityexception e1) { e1.printstacktrace(); } catch (illegalstateexception e1) { e1.printstacktrace(); } catch (ioexception e1) { e1.printstacktrace(); } try { } catch (illegalargumentexception e) { e.printstacktrace(); } catch (securityexception e) { e.printstacktrace(); } catch (illegalstateexception e) { e.printstacktrace(); } try { } catch (illegalstateexception e) { e.printstacktrace(); } } public void changeplaystate() { if(this.mp.isplaying()) this.mp.stop(); else this.mp.start(); } public static boolean isviewvisible(view subview, view parentview) { rect scrollbounds = new rect(); parentview.gethitrect(scrollbounds); if (subview.getlocalvisiblerect(scrollbounds)) { return true; } return false; }
}
to achieve thing visible items positions discussed in this thread. dont know how pass visible scroll position videoplaycontroller class , how track , access right object based on scroll position.
Comments
Post a Comment