private void CacheVideosFiles(Uri videoUrl) { try { if (Cache == null) { Cache = new SimpleCache(MainContext.CacheDir, new NoOpCacheEvictor()); } if (CacheDataSourceFactory == null) { CacheDataSourceFactory = new CacheDataSourceFactory(Cache, DefaultDataSourceFac); } var dataSpec = new DataSpec(videoUrl, 0, 3000 * 1024, null); //0, 1000 * 1024, null var counters = new CacheUtil.CachingCounters(); CacheUtil.GetCached(dataSpec, Cache, counters); if (counters.ContentLength == counters.TotalCachedBytes()) { } else if (counters.TotalCachedBytes() == 0) { // not cached at all Task.Run(() => { try { var cacheDataSource = new CacheDataSource(Cache, CacheDataSourceFactory.CreateDataSource()); CacheUtil.Cache(dataSpec, Cache, cacheDataSource, counters, new AtomicBoolean()); double downloadPercentage = counters.TotalCachedBytes() * 100d / counters.ContentLength; } catch (Exception e) { Console.WriteLine(e); } }); } else { // just few mb cached } } catch (Exception e) { Console.WriteLine(e); } }
public void PlayVideo(bool isEndOfList, Holders.VideoAdapterViewHolder holderVideoPlayer = null, PostsObject item = null) { try { if (VideoPlayer == null) { SetPlayer(); } int targetPosition; if (!isEndOfList) { var startPosition = ((LinearLayoutManager)GetLayoutManager()).FindFirstVisibleItemPosition(); var endPosition = ((LinearLayoutManager)GetLayoutManager()).FindLastVisibleItemPosition(); if (endPosition - startPosition > 1) { endPosition = startPosition + 1; } if (startPosition < 0 || endPosition < 0) { return; } if (startPosition != endPosition) { var startPositionVideoHeight = GetVisibleVideoSurfaceHeight(startPosition); var endPositionVideoHeight = GetVisibleVideoSurfaceHeight(endPosition); targetPosition = startPositionVideoHeight > endPositionVideoHeight ? startPosition : endPosition; } else { targetPosition = startPosition; } } else { targetPosition = GetAdapter().ItemCount - 1; } if (targetPosition == PlayPosition) { return; } // set the position of the list-item that is to be played PlayPosition = targetPosition; if (VideoSurfaceView == null) { return; } VideoSurfaceView.Visibility = ViewStates.Invisible; RemoveVideoView(VideoSurfaceView); var currentPosition = targetPosition - ((LinearLayoutManager)GetLayoutManager()).FindFirstVisibleItemPosition(); var child = GetChildAt(currentPosition); if (child == null) { return; } dynamic holder; if (holderVideoPlayer != null) { holder = holderVideoPlayer; targetPosition = holderVideoPlayer.LayoutPosition; } else { Holders.VideoAdapterViewHolder holderChild = (Holders.VideoAdapterViewHolder)child.Tag; if (holderChild == null) { PlayPosition = -1; return; } else { targetPosition = holderChild.LayoutPosition; holder = holderChild; } } if (!(holder is Holders.VideoAdapterViewHolder holderVideo)) { return; } MediaContainerLayout = holderVideo.MediaContainerLayout; Thumbnail = holderVideo.VideoImage; ViewHolderParent = holderVideo.ItemView; PlayControl = holderVideo.PlayControl; if (!IsVideoViewAdded) { AddVideoView(); } holderVideo.VideoProgressBar.Visibility = ViewStates.Visible; VideoSurfaceView.Player = VideoPlayer; var controlView = VideoSurfaceView.FindViewById <PlayerControlView>(Resource.Id.exo_controller); Uri videoUrl = Uri.Parse(item != null ? item.MediaSet[0].File : NativeFeedAdapter.PixelNewsFeedList[targetPosition].MediaSet[0].File); //>> Old Code //===================== Exo Player ======================== var lis = new ExoPlayerRecyclerEvent(controlView, this, holderVideo); IMediaSource videoSource = GetMediaSourceFromUrl(videoUrl, "normal"); var dataSpec = new DataSpec(videoUrl); //0, 1000 * 1024, null if (Cache == null) { CacheVideosFiles(videoUrl); } //Cache = new SimpleCache(new Java.IO.File(MainContext.FilesDir, "media"), new NoOpCacheEvictor()); if (CacheDataSourceFactory == null) { CacheDataSourceFactory = new CacheDataSourceFactory(Cache, DefaultDataSourceFac); } var counters = new CacheUtil.CachingCounters(); CacheUtil.GetCached(dataSpec, Cache, counters); if (counters.ContentLength == counters.TotalCachedBytes()) { videoSource = new ExtractorMediaSource.Factory(CacheDataSourceFactory).CreateMediaSource(videoUrl); } else if (counters.TotalCachedBytes() == 0) { videoSource = new ExtractorMediaSource.Factory(CacheDataSourceFactory).CreateMediaSource(videoUrl); // not cached at all Task.Run(() => { try { var cacheDataSource = new CacheDataSource(Cache, CacheDataSourceFactory.CreateDataSource()); CacheUtil.Cache(dataSpec, Cache, cacheDataSource, counters, new AtomicBoolean()); double downloadPercentage = counters.TotalCachedBytes() * 100d / counters.ContentLength; Console.WriteLine(downloadPercentage); } catch (Exception e) { Console.WriteLine(e); } }); } else { // partially cached videoSource = new ExtractorMediaSource.Factory(CacheDataSourceFactory).CreateMediaSource(videoUrl); } lis.mFullScreenButton.SetOnClickListener(new NewClicker(lis.mFullScreenButton, videoUrl.ToString(), this)); VideoPlayer.Prepare(videoSource); VideoPlayer.AddListener(lis); VideoPlayer.PlayWhenReady = true; } catch (Exception e) { Console.WriteLine(e); } }