Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.77 KB | None | 0 0
  1. private enum VolumeState {ON, OFF}
  2.  
  3. //iu
  4. private ImageView thumbnail, volumeControl;
  5. private ProgressBar progressBar;
  6. private View viewHolderParent;
  7. private RelativeLayout frameLayout;
  8. private PlayerView videoSurfaceView;
  9. private SimpleExoPlayer videoPlayer;
  10.  
  11. //Vars
  12. private int videoSurfaceDefaultHeight = 0;
  13. private int screenDefaultHeight = 0;
  14. private Context context;
  15. private int playPosition = -1;
  16. private boolean isVideoViewAdded;
  17. private RequestManager requestManager;
  18. private VolumeState volumeState;
  19. private List<ChildrenData> childrenDataList;
  20. private static final String TAG = "CustomRecyclerView";
  21. //controlling playback status
  22. private int currentPosition;
  23.  
  24. public CustomRecyclerView(@NonNull Context context) {
  25. super(context);
  26. init(context);
  27. }
  28.  
  29. public CustomRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
  30. super(context, attrs);
  31. init(context);
  32. }
  33.  
  34. //------------------------------------------------------------------------------------------//
  35.  
  36. public void init(final Context context) {
  37.  
  38. //Getting the size of the display
  39. this.context = context.getApplicationContext();
  40. Display display = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
  41. Point point = new Point();
  42. display.getSize(point);
  43. videoSurfaceDefaultHeight = point.x;
  44. screenDefaultHeight = point.y;
  45.  
  46. videoSurfaceView = new PlayerView(this.context);
  47. videoSurfaceView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_ZOOM);
  48.  
  49. BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
  50. TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);
  51. TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
  52.  
  53. //Create the player
  54. videoPlayer = ExoPlayerFactory.newSimpleInstance(context, trackSelector);
  55.  
  56. //Bind the player to the view
  57. videoSurfaceView.setUseController(true);
  58. videoSurfaceView.setPlayer(videoPlayer);
  59. setVolumeControl(VolumeState.ON);
  60.  
  61. //------------------------------------------------------------------------------------------//
  62.  
  63. addOnScrollListener(new RecyclerView.OnScrollListener() {
  64. @Override
  65. public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
  66. super.onScrollStateChanged(recyclerView, newState);
  67.  
  68. if (newState == RecyclerView.SCROLL_STATE_IDLE) {
  69.  
  70. if (thumbnail != null) {
  71. thumbnail.setVisibility(VISIBLE);
  72.  
  73. }
  74.  
  75. // There's a special case when the end of the list has been reached.
  76. // Need to handle that with this bit of logic
  77.  
  78. if (recyclerView.canScrollVertically(1)) {
  79. playVideo(false);
  80. } else {
  81. playVideo(true);
  82. }
  83.  
  84. }
  85. }
  86.  
  87. @Override
  88. public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
  89. super.onScrolled(recyclerView, dx, dy);
  90. }
  91. });
  92.  
  93. //------------------------------------------------------------------------------------------//
  94.  
  95. addOnChildAttachStateChangeListener(new OnChildAttachStateChangeListener() {
  96. @Override
  97. public void onChildViewAttachedToWindow(@NonNull View view) {
  98.  
  99. }
  100.  
  101. @Override
  102. public void onChildViewDetachedFromWindow(@NonNull View view) {
  103. if (viewHolderParent != null && viewHolderParent.equals(view)) {
  104. resetVideoView();
  105. }
  106. }
  107. });
  108.  
  109.  
  110. //------------------------------------------------------------------------------------------//
  111.  
  112. videoPlayer.addListener(new Player.EventListener() {
  113. @Override
  114. public void onTimelineChanged(Timeline timeline, @Nullable Object manifest, int reason) {
  115.  
  116. }
  117.  
  118. @Override
  119. public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) {
  120.  
  121. }
  122.  
  123. @Override
  124. public void onLoadingChanged(boolean isLoading) {
  125.  
  126. }
  127.  
  128. @Override
  129. public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
  130. switch (playbackState) {
  131. case Player.STATE_BUFFERING: {
  132. Log.e(TAG, "onPlayerStateChanged: BUFFERING VIDEO");
  133. if (progressBar != null) {
  134. progressBar.setVisibility(VISIBLE);
  135. }
  136. break;
  137. }
  138. case Player.STATE_ENDED: {
  139. Log.d(TAG, "onPlayerStateChanged: Video ended.");
  140. videoPlayer.seekTo(0);
  141. break;
  142. }
  143.  
  144. case Player.STATE_IDLE: {
  145. Log.d(TAG, "onPlayerStateChanged: idle");
  146. break;
  147. }
  148.  
  149. case Player.STATE_READY: {
  150. Log.d(TAG, "onPlayerStateChanged: Ready to play");
  151. if (progressBar != null)
  152. progressBar.setVisibility(GONE);
  153. if (!isVideoViewAdded)
  154. addVideoView();
  155. break;
  156. }
  157.  
  158. default:
  159. break;
  160. }
  161.  
  162. }
  163.  
  164. @Override
  165. public void onRepeatModeChanged(int repeatMode) {
  166.  
  167. }
  168.  
  169. @Override
  170. public void onShuffleModeEnabledChanged(boolean shuffleModeEnabled) {
  171.  
  172. }
  173.  
  174. @Override
  175. public void onPlayerError(ExoPlaybackException error) {
  176.  
  177. }
  178.  
  179. @Override
  180. public void onPositionDiscontinuity(int reason) {
  181.  
  182. }
  183.  
  184. @Override
  185. public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) {
  186.  
  187. }
  188.  
  189. @Override
  190. public void onSeekProcessed() {
  191.  
  192. }
  193. });
  194.  
  195. }
  196.  
  197. //------------------------------------------------------------------------------------------//
  198.  
  199. public void playVideo(Boolean isEndOfList) {
  200. int targetPosition;
  201.  
  202. if (!isEndOfList) {
  203. int startPosition = ((LinearLayoutManager) Objects.requireNonNull(getLayoutManager())).findFirstVisibleItemPosition();
  204. int endPosition = ((LinearLayoutManager) getLayoutManager()).findLastVisibleItemPosition();
  205.  
  206. //More than 2 posts
  207. if (endPosition - startPosition > 1) {
  208. endPosition = ++startPosition;
  209. }
  210.  
  211. //Error Checking
  212. if (endPosition < 0 || startPosition < 0) {
  213. Log.d(TAG, "playVideo: out of bounds(negative) error");
  214. return;
  215. }
  216.  
  217. //More than 1 post
  218. if (startPosition != endPosition) {
  219. int startPositionHeight = getVisibleSurface(startPosition);
  220. int endPositionHeight = getVisibleSurface(endPosition);
  221.  
  222. targetPosition = startPositionHeight > endPositionHeight ? startPosition : endPosition;
  223. Log.d(TAG, "targetPosition: "+ targetPosition);
  224. } else {
  225. targetPosition = startPosition;
  226. Log.d(TAG, "targetPosition: "+ targetPosition);
  227. }
  228.  
  229. if (targetPosition == playPosition) {
  230. Log.d(TAG, "playVideo: video already playing");
  231. return;
  232. }
  233.  
  234. Log.d(TAG, "playVideo: playPosition" + playPosition);
  235. playPosition = targetPosition;
  236. Log.d(TAG, "playVideo: playPosition" + playPosition);
  237.  
  238. //Setting the position of the post to be displayed
  239. if (videoSurfaceView == null) {
  240. Log.d(TAG, "playVideo: videoSurfaceView is null");
  241. return;
  242. }
  243.  
  244.  
  245.  
  246. if (childrenDataList.get(targetPosition).getDataModel().getPostHint() != null) {
  247. if (childrenDataList.get(targetPosition).getDataModel().getPostHint().equals("hosted:video")) {
  248.  
  249. videoSurfaceView.setVisibility(INVISIBLE);
  250. removeVideoView(videoSurfaceView);
  251.  
  252. currentPosition = targetPosition - ((LinearLayoutManager) getLayoutManager()).findFirstVisibleItemPosition();
  253.  
  254. View child = getChildAt(currentPosition);
  255. if (child == null) {
  256. Log.d(TAG, "playVideo: child at currentPosition is null");
  257. return;
  258. }
  259.  
  260. VideoViewHolder holder = (VideoViewHolder) child.getTag();
  261. if (holder == null) {
  262. Log.e(TAG, "playVideo: holder is null");
  263. return;
  264. }
  265.  
  266. thumbnail = holder.thumbnail;
  267. progressBar = holder.progressBar;
  268. volumeControl = holder.volumeControl;
  269. viewHolderParent = holder.itemView;
  270. frameLayout = holder.itemView.findViewById(R.id.container);
  271. requestManager = holder.requestManager;
  272. videoSurfaceView.setPlayer(videoPlayer);
  273.  
  274. viewHolderParent.setOnClickListener(videoViewClickListener);
  275.  
  276. DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(context, Util.getUserAgent(context, "DesignForReddit"));
  277. String mediaUrl = childrenDataList.get(targetPosition).getDataModel().getSecureMedia().getRedditVideo().getFallbackUrl();
  278.  
  279. if (mediaUrl != null) {
  280. MediaSource videoSource = new ExtractorMediaSource.Factory(dataSourceFactory)
  281. .createMediaSource(Uri.parse(mediaUrl));
  282. videoPlayer.prepare(videoSource);
  283. videoPlayer.setPlayWhenReady(true);
  284.  
  285. }
  286. }
  287. }
  288. }
  289.  
  290. }
  291. //------------------------------------------------------------------------------------------//
  292.  
  293. private OnClickListener videoViewClickListener = new OnClickListener() {
  294. @Override
  295. public void onClick(View v) {
  296. toggleVolume();
  297. }
  298. };
  299. //------------------------------------------------------------------------------------------//
  300.  
  301. private int getVisibleSurface(int playPosition) {
  302. int at = playPosition - ((LinearLayoutManager) getLayoutManager()).findFirstVisibleItemPosition();
  303. Log.d(TAG, "getVisibleSurface: at:" + at);
  304.  
  305. View child = getChildAt(at);
  306. if (child == null) {
  307. Log.d(TAG, "getVisibleSurface: child is null");
  308. return 0;
  309. }
  310.  
  311. int[] location = new int[2];
  312. child.getLocationInWindow(location);
  313.  
  314. if (location[1] < 0) {
  315. return location[1] + videoSurfaceDefaultHeight;
  316. } else {
  317. return screenDefaultHeight - location[1];
  318. }
  319. }
  320.  
  321. //------------------------------------------------------------------------------------------//
  322.  
  323. //Remove the old Player
  324. private void removeVideoView(PlayerView videoView) {
  325. ViewGroup parent = (ViewGroup) videoView.getParent();
  326.  
  327. if (parent == null) {
  328. Log.d(TAG, "removeVideoView: parent is null");
  329. return;
  330. }
  331.  
  332. int index = parent.indexOfChild(videoView);
  333. if (index >= 0) {
  334. parent.removeViewAt(index);
  335. isVideoViewAdded = false;
  336. viewHolderParent.setOnClickListener(null);
  337. }
  338. }
  339.  
  340. //------------------------------------------------------------------------------------------//
  341.  
  342. private void addVideoView() {
  343. frameLayout.addView(videoSurfaceView);
  344. isVideoViewAdded = true;
  345. videoSurfaceView.requestFocus();
  346. videoSurfaceView.setVisibility(VISIBLE);
  347. videoSurfaceView.setAlpha(1);
  348. thumbnail.setVisibility(GONE);
  349. }
  350.  
  351. //------------------------------------------------------------------------------------------//
  352.  
  353. private void resetVideoView() {
  354. if (isVideoViewAdded) {
  355. removeVideoView(videoSurfaceView);
  356. playPosition = -1;
  357. videoSurfaceView.setVisibility(INVISIBLE);
  358. thumbnail.setVisibility(VISIBLE);
  359. }
  360. }
  361.  
  362. //------------------------------------------------------------------------------------------//
  363.  
  364. public void releasePlayer() {
  365.  
  366. if (videoPlayer != null) {
  367. videoPlayer.release();
  368. videoPlayer = null;
  369. }
  370.  
  371. viewHolderParent = null;
  372. }
  373.  
  374. //------------------------------------------------------------------------------------------//
  375.  
  376. private void toggleVolume() {
  377. if (videoPlayer != null) {
  378. if (volumeState == VolumeState.OFF)
  379. setVolumeControl(VolumeState.ON);
  380. else if (volumeState == VolumeState.ON)
  381. setVolumeControl(VolumeState.OFF);
  382. }
  383. }
  384.  
  385. //------------------------------------------------------------------------------------------//
  386.  
  387. private void setVolumeControl(VolumeState state) {
  388. volumeState = state;
  389. if (state == VolumeState.OFF) {
  390. videoPlayer.setVolume(0f);
  391. animateVolumeControl();
  392. } else if (state == VolumeState.ON) {
  393. videoPlayer.setVolume(1f);
  394. animateVolumeControl();
  395. }
  396. }
  397.  
  398. //------------------------------------------------------------------------------------------//
  399.  
  400. private void animateVolumeControl() {
  401.  
  402. if (volumeControl != null) {
  403. volumeControl.bringToFront();
  404. if (volumeState == VolumeState.OFF) {
  405. requestManager.load(R.drawable.ic_volume_off_grey_24dp)
  406. .into(volumeControl);
  407. } else if (volumeState == VolumeState.ON) {
  408. requestManager.load(R.drawable.ic_volume_up_grey_24dp)
  409. .into(volumeControl);
  410. }
  411. volumeControl.animate().cancel();
  412.  
  413. volumeControl.setAlpha(1f);
  414.  
  415. volumeControl.animate()
  416. .alpha(0f)
  417. .setDuration(600).setStartDelay(1000);
  418. }
  419. }
  420.  
  421. //------------------------------------------------------------------------------------------//
  422.  
  423. public void setChildrenDataList(List<ChildrenData> childrenDataList) {
  424. this.childrenDataList = childrenDataList;
  425. }
  426.  
  427. public TextView subReddit;
  428. public TextView username;
  429. public TextView title;
  430. public ImageView thumbnail;
  431. public ProgressBar progressBar;
  432. public ImageView volumeControl;
  433. public RequestManager requestManager;
  434. public RelativeLayout mediaContainer;
  435. public View parent;
  436. public TextView score;
  437. public TextView commentButton;
  438. public ImageButton shareButton;
  439. public ImageButton upVoteButton;
  440. public ImageButton downVoteButton;
  441.  
  442.  
  443. public VideoViewHolder(@NonNull View itemView) {
  444. super(itemView);
  445. parent = itemView;
  446. subReddit = itemView.findViewById(R.id.subReddit);
  447. title = itemView.findViewById(R.id.title);
  448. username = itemView.findViewById(R.id.username);
  449. mediaContainer = itemView.findViewById(R.id.container);
  450. thumbnail = itemView.findViewById(R.id.thumbnail);
  451. commentButton = itemView.findViewById(R.id.commentButton);
  452. progressBar = itemView.findViewById(R.id.progressBar);
  453. shareButton = itemView.findViewById(R.id.shareButton);
  454. upVoteButton = itemView.findViewById(R.id.upVoteButton);
  455. downVoteButton = itemView.findViewById(R.id.downVoteButton);
  456. score = itemView.findViewById(R.id.score);
  457. volumeControl = itemView.findViewById(R.id.volumeControl);
  458. }
  459.  
  460. public void onBind(String url, RequestManager requestManager){
  461. this.requestManager = requestManager;
  462. parent.setTag(this);
  463. this.requestManager
  464. .load(url)
  465. .into(thumbnail);
  466.  
  467. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement