I implemented recyclerview in which I add texture views as list items to play videos from a URL. Now, as an application for the vine and instagram, I want to play the video when it is visible in recycliewiew mode and stop / pause the video when the listitem exits the screen. Below is my code:
Adapter video 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(); *
}
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);
}
To achieve this, I get visible position positions, as discussed in this thread. But I donβt know how to pass the visible scroll position to my VideoPlayController class and how to track and access an object based on the scroll position.
source share