Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private enum VolumeState {ON, OFF}
- //iu
- private ImageView thumbnail, volumeControl;
- private ProgressBar progressBar;
- private View viewHolderParent;
- private RelativeLayout frameLayout;
- private PlayerView videoSurfaceView;
- private SimpleExoPlayer videoPlayer;
- //Vars
- private int videoSurfaceDefaultHeight = 0;
- private int screenDefaultHeight = 0;
- private Context context;
- private int playPosition = -1;
- private boolean isVideoViewAdded;
- private RequestManager requestManager;
- private VolumeState volumeState;
- private List<ChildrenData> childrenDataList;
- private static final String TAG = "CustomRecyclerView";
- //controlling playback status
- private int currentPosition;
- public CustomRecyclerView(@NonNull Context context) {
- super(context);
- init(context);
- }
- public CustomRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
- super(context, attrs);
- init(context);
- }
- //------------------------------------------------------------------------------------------//
- public void init(final Context context) {
- //Getting the size of the display
- this.context = context.getApplicationContext();
- Display display = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
- Point point = new Point();
- display.getSize(point);
- videoSurfaceDefaultHeight = point.x;
- screenDefaultHeight = point.y;
- videoSurfaceView = new PlayerView(this.context);
- videoSurfaceView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_ZOOM);
- BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
- TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);
- TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
- //Create the player
- videoPlayer = ExoPlayerFactory.newSimpleInstance(context, trackSelector);
- //Bind the player to the view
- videoSurfaceView.setUseController(true);
- videoSurfaceView.setPlayer(videoPlayer);
- setVolumeControl(VolumeState.ON);
- //------------------------------------------------------------------------------------------//
- addOnScrollListener(new RecyclerView.OnScrollListener() {
- @Override
- public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
- super.onScrollStateChanged(recyclerView, newState);
- if (newState == RecyclerView.SCROLL_STATE_IDLE) {
- if (thumbnail != null) {
- thumbnail.setVisibility(VISIBLE);
- }
- // There's a special case when the end of the list has been reached.
- // Need to handle that with this bit of logic
- if (recyclerView.canScrollVertically(1)) {
- playVideo(false);
- } else {
- playVideo(true);
- }
- }
- }
- @Override
- public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
- super.onScrolled(recyclerView, dx, dy);
- }
- });
- //------------------------------------------------------------------------------------------//
- addOnChildAttachStateChangeListener(new OnChildAttachStateChangeListener() {
- @Override
- public void onChildViewAttachedToWindow(@NonNull View view) {
- }
- @Override
- public void onChildViewDetachedFromWindow(@NonNull View view) {
- if (viewHolderParent != null && viewHolderParent.equals(view)) {
- resetVideoView();
- }
- }
- });
- //------------------------------------------------------------------------------------------//
- videoPlayer.addListener(new Player.EventListener() {
- @Override
- public void onTimelineChanged(Timeline timeline, @Nullable Object manifest, int reason) {
- }
- @Override
- public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) {
- }
- @Override
- public void onLoadingChanged(boolean isLoading) {
- }
- @Override
- public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
- switch (playbackState) {
- case Player.STATE_BUFFERING: {
- Log.e(TAG, "onPlayerStateChanged: BUFFERING VIDEO");
- if (progressBar != null) {
- progressBar.setVisibility(VISIBLE);
- }
- break;
- }
- case Player.STATE_ENDED: {
- Log.d(TAG, "onPlayerStateChanged: Video ended.");
- videoPlayer.seekTo(0);
- break;
- }
- case Player.STATE_IDLE: {
- Log.d(TAG, "onPlayerStateChanged: idle");
- break;
- }
- case Player.STATE_READY: {
- Log.d(TAG, "onPlayerStateChanged: Ready to play");
- if (progressBar != null)
- progressBar.setVisibility(GONE);
- if (!isVideoViewAdded)
- addVideoView();
- break;
- }
- default:
- break;
- }
- }
- @Override
- public void onRepeatModeChanged(int repeatMode) {
- }
- @Override
- public void onShuffleModeEnabledChanged(boolean shuffleModeEnabled) {
- }
- @Override
- public void onPlayerError(ExoPlaybackException error) {
- }
- @Override
- public void onPositionDiscontinuity(int reason) {
- }
- @Override
- public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) {
- }
- @Override
- public void onSeekProcessed() {
- }
- });
- }
- //------------------------------------------------------------------------------------------//
- public void playVideo(Boolean isEndOfList) {
- int targetPosition;
- if (!isEndOfList) {
- int startPosition = ((LinearLayoutManager) Objects.requireNonNull(getLayoutManager())).findFirstVisibleItemPosition();
- int endPosition = ((LinearLayoutManager) getLayoutManager()).findLastVisibleItemPosition();
- //More than 2 posts
- if (endPosition - startPosition > 1) {
- endPosition = ++startPosition;
- }
- //Error Checking
- if (endPosition < 0 || startPosition < 0) {
- Log.d(TAG, "playVideo: out of bounds(negative) error");
- return;
- }
- //More than 1 post
- if (startPosition != endPosition) {
- int startPositionHeight = getVisibleSurface(startPosition);
- int endPositionHeight = getVisibleSurface(endPosition);
- targetPosition = startPositionHeight > endPositionHeight ? startPosition : endPosition;
- Log.d(TAG, "targetPosition: "+ targetPosition);
- } else {
- targetPosition = startPosition;
- Log.d(TAG, "targetPosition: "+ targetPosition);
- }
- if (targetPosition == playPosition) {
- Log.d(TAG, "playVideo: video already playing");
- return;
- }
- Log.d(TAG, "playVideo: playPosition" + playPosition);
- playPosition = targetPosition;
- Log.d(TAG, "playVideo: playPosition" + playPosition);
- //Setting the position of the post to be displayed
- if (videoSurfaceView == null) {
- Log.d(TAG, "playVideo: videoSurfaceView is null");
- return;
- }
- if (childrenDataList.get(targetPosition).getDataModel().getPostHint() != null) {
- if (childrenDataList.get(targetPosition).getDataModel().getPostHint().equals("hosted:video")) {
- videoSurfaceView.setVisibility(INVISIBLE);
- removeVideoView(videoSurfaceView);
- currentPosition = targetPosition - ((LinearLayoutManager) getLayoutManager()).findFirstVisibleItemPosition();
- View child = getChildAt(currentPosition);
- if (child == null) {
- Log.d(TAG, "playVideo: child at currentPosition is null");
- return;
- }
- VideoViewHolder holder = (VideoViewHolder) child.getTag();
- if (holder == null) {
- Log.e(TAG, "playVideo: holder is null");
- return;
- }
- thumbnail = holder.thumbnail;
- progressBar = holder.progressBar;
- volumeControl = holder.volumeControl;
- viewHolderParent = holder.itemView;
- frameLayout = holder.itemView.findViewById(R.id.container);
- requestManager = holder.requestManager;
- videoSurfaceView.setPlayer(videoPlayer);
- viewHolderParent.setOnClickListener(videoViewClickListener);
- DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(context, Util.getUserAgent(context, "DesignForReddit"));
- String mediaUrl = childrenDataList.get(targetPosition).getDataModel().getSecureMedia().getRedditVideo().getFallbackUrl();
- if (mediaUrl != null) {
- MediaSource videoSource = new ExtractorMediaSource.Factory(dataSourceFactory)
- .createMediaSource(Uri.parse(mediaUrl));
- videoPlayer.prepare(videoSource);
- videoPlayer.setPlayWhenReady(true);
- }
- }
- }
- }
- }
- //------------------------------------------------------------------------------------------//
- private OnClickListener videoViewClickListener = new OnClickListener() {
- @Override
- public void onClick(View v) {
- toggleVolume();
- }
- };
- //------------------------------------------------------------------------------------------//
- private int getVisibleSurface(int playPosition) {
- int at = playPosition - ((LinearLayoutManager) getLayoutManager()).findFirstVisibleItemPosition();
- Log.d(TAG, "getVisibleSurface: at:" + at);
- View child = getChildAt(at);
- if (child == null) {
- Log.d(TAG, "getVisibleSurface: child is null");
- return 0;
- }
- int[] location = new int[2];
- child.getLocationInWindow(location);
- if (location[1] < 0) {
- return location[1] + videoSurfaceDefaultHeight;
- } else {
- return screenDefaultHeight - location[1];
- }
- }
- //------------------------------------------------------------------------------------------//
- //Remove the old Player
- private void removeVideoView(PlayerView videoView) {
- ViewGroup parent = (ViewGroup) videoView.getParent();
- if (parent == null) {
- Log.d(TAG, "removeVideoView: parent is null");
- return;
- }
- int index = parent.indexOfChild(videoView);
- if (index >= 0) {
- parent.removeViewAt(index);
- isVideoViewAdded = false;
- viewHolderParent.setOnClickListener(null);
- }
- }
- //------------------------------------------------------------------------------------------//
- private void addVideoView() {
- frameLayout.addView(videoSurfaceView);
- isVideoViewAdded = true;
- videoSurfaceView.requestFocus();
- videoSurfaceView.setVisibility(VISIBLE);
- videoSurfaceView.setAlpha(1);
- thumbnail.setVisibility(GONE);
- }
- //------------------------------------------------------------------------------------------//
- private void resetVideoView() {
- if (isVideoViewAdded) {
- removeVideoView(videoSurfaceView);
- playPosition = -1;
- videoSurfaceView.setVisibility(INVISIBLE);
- thumbnail.setVisibility(VISIBLE);
- }
- }
- //------------------------------------------------------------------------------------------//
- public void releasePlayer() {
- if (videoPlayer != null) {
- videoPlayer.release();
- videoPlayer = null;
- }
- viewHolderParent = null;
- }
- //------------------------------------------------------------------------------------------//
- private void toggleVolume() {
- if (videoPlayer != null) {
- if (volumeState == VolumeState.OFF)
- setVolumeControl(VolumeState.ON);
- else if (volumeState == VolumeState.ON)
- setVolumeControl(VolumeState.OFF);
- }
- }
- //------------------------------------------------------------------------------------------//
- private void setVolumeControl(VolumeState state) {
- volumeState = state;
- if (state == VolumeState.OFF) {
- videoPlayer.setVolume(0f);
- animateVolumeControl();
- } else if (state == VolumeState.ON) {
- videoPlayer.setVolume(1f);
- animateVolumeControl();
- }
- }
- //------------------------------------------------------------------------------------------//
- private void animateVolumeControl() {
- if (volumeControl != null) {
- volumeControl.bringToFront();
- if (volumeState == VolumeState.OFF) {
- requestManager.load(R.drawable.ic_volume_off_grey_24dp)
- .into(volumeControl);
- } else if (volumeState == VolumeState.ON) {
- requestManager.load(R.drawable.ic_volume_up_grey_24dp)
- .into(volumeControl);
- }
- volumeControl.animate().cancel();
- volumeControl.setAlpha(1f);
- volumeControl.animate()
- .alpha(0f)
- .setDuration(600).setStartDelay(1000);
- }
- }
- //------------------------------------------------------------------------------------------//
- public void setChildrenDataList(List<ChildrenData> childrenDataList) {
- this.childrenDataList = childrenDataList;
- }
- public TextView subReddit;
- public TextView username;
- public TextView title;
- public ImageView thumbnail;
- public ProgressBar progressBar;
- public ImageView volumeControl;
- public RequestManager requestManager;
- public RelativeLayout mediaContainer;
- public View parent;
- public TextView score;
- public TextView commentButton;
- public ImageButton shareButton;
- public ImageButton upVoteButton;
- public ImageButton downVoteButton;
- public VideoViewHolder(@NonNull View itemView) {
- super(itemView);
- parent = itemView;
- subReddit = itemView.findViewById(R.id.subReddit);
- title = itemView.findViewById(R.id.title);
- username = itemView.findViewById(R.id.username);
- mediaContainer = itemView.findViewById(R.id.container);
- thumbnail = itemView.findViewById(R.id.thumbnail);
- commentButton = itemView.findViewById(R.id.commentButton);
- progressBar = itemView.findViewById(R.id.progressBar);
- shareButton = itemView.findViewById(R.id.shareButton);
- upVoteButton = itemView.findViewById(R.id.upVoteButton);
- downVoteButton = itemView.findViewById(R.id.downVoteButton);
- score = itemView.findViewById(R.id.score);
- volumeControl = itemView.findViewById(R.id.volumeControl);
- }
- public void onBind(String url, RequestManager requestManager){
- this.requestManager = requestManager;
- parent.setTag(this);
- this.requestManager
- .load(url)
- .into(thumbnail);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement