From 3b5aa08783adaf38e2b471c11160619db191a045 Mon Sep 17 00:00:00 2001 From: medyas Date: Thu, 18 Oct 2018 19:55:06 +0100 Subject: [PATCH] Fixed video seek on rotation --- .idea/caches/build_file_checksums.ser | Bin 547 -> 547 bytes app/build.gradle | 5 + .../bakingapp/Classes/ExoPlayerManager.java | 122 ++++++++++++++++++ .../Fragments/RecipeDetailViewFragment.java | 61 +++++---- .../layout/fragment_recipe_detail_view.xml | 4 +- 5 files changed, 167 insertions(+), 25 deletions(-) create mode 100644 app/src/main/java/ml/medyas/bakingapp/Classes/ExoPlayerManager.java diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser index 8fa24eb54e91a9b5d94f105d7b1431a4da2ec20f..eae947532202ae5e3186713b221b134c3253e1f7 100644 GIT binary patch delta 35 tcmV+;0Nnqh1fv9ym;{JBFZhw1cM#s8Hd34J^qm4$@75Jkg0RbaZ5M=-W delta 35 tcmV+;0Nnqh1fv9ym;{4lNVbujcMt&Y9@9C%gUTA~#yqu3jeese0Ra&K4-)_Y diff --git a/app/build.gradle b/app/build.gradle index d19440a..d23e9aa 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -59,6 +59,11 @@ dependencies { // Exoplayer Audio Video player implementation 'com.google.android.exoplayer:exoplayer:2.8.4' + // Base implementation with a standard SeekBar + implementation 'com.github.rubensousa:previewseekbar:2.0.0' + + // ExoPlayer extension that contains a TimeBar. + implementation 'com.github.rubensousa:previewseekbar-exoplayer:2.8.1.0' // Picasso image manipulation Library implementation 'com.squareup.picasso:picasso:2.71828' diff --git a/app/src/main/java/ml/medyas/bakingapp/Classes/ExoPlayerManager.java b/app/src/main/java/ml/medyas/bakingapp/Classes/ExoPlayerManager.java new file mode 100644 index 0000000..eafd904 --- /dev/null +++ b/app/src/main/java/ml/medyas/bakingapp/Classes/ExoPlayerManager.java @@ -0,0 +1,122 @@ +package ml.medyas.bakingapp.Classes; + + +/* + * Copyright 2016 The Android Open Source Project + * Copyright 2017 RĂºben Sousa + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +import android.content.Context; +import android.net.Uri; +import android.widget.ImageView; + +import com.google.android.exoplayer2.DefaultLoadControl; +import com.google.android.exoplayer2.DefaultRenderersFactory; +import com.google.android.exoplayer2.ExoPlayerFactory; +import com.google.android.exoplayer2.LoadControl; +import com.google.android.exoplayer2.SimpleExoPlayer; +import com.google.android.exoplayer2.source.ExtractorMediaSource; +import com.google.android.exoplayer2.source.MediaSource; +import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection; +import com.google.android.exoplayer2.trackselection.DefaultTrackSelector; +import com.google.android.exoplayer2.trackselection.TrackSelection; +import com.google.android.exoplayer2.trackselection.TrackSelector; +import com.google.android.exoplayer2.ui.PlayerView; +import com.google.android.exoplayer2.upstream.DataSource; +import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter; +import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory; +import com.google.android.exoplayer2.util.Util; + + +public class ExoPlayerManager { + + private MediaSource mediaSourceBuilder; + private PlayerView playerView; + private SimpleExoPlayer player; + private String thumbnailsUrl; + private ImageView imageView; + private Context context; + + + public ExoPlayerManager(PlayerView playerView, Context context) { + this.playerView = playerView; + this.context = context; + } + + public void play(Uri uri) { + // Produces DataSource instances through which media data is loaded. + DataSource.Factory dataSourceFactory = + new DefaultDataSourceFactory(context, Util.getUserAgent(context, "BackingApp")); + this.mediaSourceBuilder = new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(uri); + } + + public SimpleExoPlayer getPlayer() { + return player; + } + + public void onStart() { + if (Util.SDK_INT > 23) { + createPlayers(); + } + } + + public void onResume() { + if (Util.SDK_INT <= 23) { + createPlayers(); + } + } + + public void onPause() { + if (Util.SDK_INT <= 23) { + releasePlayers(); + } + } + + public void onStop() { + if (Util.SDK_INT > 23) { + releasePlayers(); + } + } + + + private void releasePlayers() { + if (player != null) { + player.release(); + player = null; + } + } + + private void createPlayers() { + if (player != null) { + player.release(); + } + player = createFullPlayer(); + playerView.setPlayer(player); + } + + private SimpleExoPlayer createFullPlayer() { + TrackSelection.Factory videoTrackSelectionFactory + = new AdaptiveTrackSelection.Factory(new DefaultBandwidthMeter()); + TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory); + LoadControl loadControl = new DefaultLoadControl(); + SimpleExoPlayer player = ExoPlayerFactory.newSimpleInstance( + new DefaultRenderersFactory(playerView.getContext()), + trackSelector, loadControl); + player.setPlayWhenReady(false); + player.prepare(mediaSourceBuilder); + return player; + } +} \ No newline at end of file diff --git a/app/src/main/java/ml/medyas/bakingapp/Fragments/RecipeDetailViewFragment.java b/app/src/main/java/ml/medyas/bakingapp/Fragments/RecipeDetailViewFragment.java index 1fd5d8c..5f4a9c2 100644 --- a/app/src/main/java/ml/medyas/bakingapp/Fragments/RecipeDetailViewFragment.java +++ b/app/src/main/java/ml/medyas/bakingapp/Fragments/RecipeDetailViewFragment.java @@ -6,6 +6,7 @@ import android.net.Uri; import android.os.Bundle; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; @@ -14,24 +15,13 @@ import android.widget.LinearLayout; import android.widget.TextView; -import com.google.android.exoplayer2.ExoPlayerFactory; import com.google.android.exoplayer2.SimpleExoPlayer; -import com.google.android.exoplayer2.source.ExtractorMediaSource; -import com.google.android.exoplayer2.source.MediaSource; -import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection; -import com.google.android.exoplayer2.trackselection.DefaultTrackSelector; -import com.google.android.exoplayer2.trackselection.TrackSelection; -import com.google.android.exoplayer2.trackselection.TrackSelector; import com.google.android.exoplayer2.ui.PlayerView; -import com.google.android.exoplayer2.upstream.BandwidthMeter; -import com.google.android.exoplayer2.upstream.DataSource; -import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter; -import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory; -import com.google.android.exoplayer2.util.Util; import butterknife.BindView; import butterknife.ButterKnife; import butterknife.OnClick; +import ml.medyas.bakingapp.Classes.ExoPlayerManager; import ml.medyas.bakingapp.Classes.StepsClass; import ml.medyas.bakingapp.R; @@ -42,10 +32,12 @@ public class RecipeDetailViewFragment extends Fragment { public static final String PLAY_WHEN_READY = "play_when_ready"; public static final String PLAYBACK_POSITION = "playback_position"; + public static final String thumb = "https://d2v9y0dukr6mq2.cloudfront.net/video/thumbnail/itCjTBE/loading-waiting-web-symbol-element-black-and-white-motion-design-video-looping-animation-hd-1920x1080_baa7wxg__F0000.png"; @BindView(R.id.video_view) PlayerView playerView; @BindView(R.id.view_step_title) TextView title; @BindView(R.id.view_step_desc) TextView desc; + private SimpleExoPlayer player; private long playbackPosition = 0; private int currentWindow; @@ -53,6 +45,8 @@ public class RecipeDetailViewFragment extends Fragment { private OnSlideListener mListener; + private ExoPlayerManager exoPlayerManager; + public RecipeDetailViewFragment() { // Required empty public constructor } @@ -98,9 +92,22 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, getScreenHeight())); } + + exoPlayerManager = new ExoPlayerManager(playerView, getActivity().getApplicationContext()); + if(!step.getVideoURL().equals("")) { + Uri videoUri = Uri.parse(step.getVideoURL()); + exoPlayerManager.play(videoUri); + } + return root; } + @Override + public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + + } + @OnClick({R.id.view_swipe_left, R.id.view_swipe_right}) public void swipeFragments(ImageView img) { int id = img.getId(); @@ -114,34 +121,35 @@ public void swipeFragments(ImageView img) { @Override public void onStart() { super.onStart(); - if ((player == null)) { - initializePlayer(); - } + + exoPlayerManager.onStart(); } @Override public void onResume() { super.onResume(); - //hideSystemUi(); - if ((player == null)) { - initializePlayer(); - } + exoPlayerManager.onResume(); + + exoPlayerManager.getPlayer().setPlayWhenReady(playWhenReady); + exoPlayerManager.getPlayer().seekTo(playbackPosition); } @Override public void onPause() { super.onPause(); - releasePlayer(); + playbackPosition = exoPlayerManager.getPlayer().getCurrentPosition(); + playWhenReady = exoPlayerManager.getPlayer().getPlayWhenReady(); + exoPlayerManager.onPause(); } @Override public void onStop() { super.onStop(); - releasePlayer(); + exoPlayerManager.onStop(); } private void initializePlayer() { - BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter(); + /*BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter(); TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter); TrackSelector trackSelector = @@ -149,8 +157,6 @@ private void initializePlayer() { //Initialize the player player = ExoPlayerFactory.newSimpleInstance(getActivity().getApplicationContext(), trackSelector); - player.seekTo(playbackPosition); - player.setPlayWhenReady(playWhenReady); //Initialize simpleExoPlayerView playerView.setPlayer(player); @@ -166,7 +172,14 @@ private void initializePlayer() { // Prepare the player with the source. player.prepare(videoSource); + + + player.seekTo(playbackPosition); + player.setPlayWhenReady(playWhenReady); } + */ + + } private void releasePlayer() { diff --git a/app/src/main/res/layout/fragment_recipe_detail_view.xml b/app/src/main/res/layout/fragment_recipe_detail_view.xml index f6a8b10..1a92afe 100644 --- a/app/src/main/res/layout/fragment_recipe_detail_view.xml +++ b/app/src/main/res/layout/fragment_recipe_detail_view.xml @@ -11,7 +11,9 @@ + android:layout_height="400dp" + app:fastforward_increment="1000" + app:rewind_increment="1000"/>