From e51fb0b2902cd8d6a1393d506a5cd47a7688f6a2 Mon Sep 17 00:00:00 2001 From: Jannik Date: Sat, 7 Nov 2020 13:33:59 +0100 Subject: [PATCH] add remaining time to player --- .../java/org/mosad/teapod/PlayerActivity.kt | 32 +++++++++++++------ app/src/main/res/layout/activity_player.xml | 1 + app/src/main/res/layout/player_controls.xml | 6 ++-- app/src/main/res/values/strings.xml | 2 ++ 4 files changed, 29 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/org/mosad/teapod/PlayerActivity.kt b/app/src/main/java/org/mosad/teapod/PlayerActivity.kt index 5bc908a..2f02383 100644 --- a/app/src/main/java/org/mosad/teapod/PlayerActivity.kt +++ b/app/src/main/java/org/mosad/teapod/PlayerActivity.kt @@ -8,10 +8,7 @@ import android.view.View import android.view.WindowInsets import android.view.WindowInsetsController import androidx.appcompat.app.AppCompatActivity -import com.google.android.exoplayer2.ExoPlayer -import com.google.android.exoplayer2.MediaItem -import com.google.android.exoplayer2.Player -import com.google.android.exoplayer2.SimpleExoPlayer +import com.google.android.exoplayer2.* import com.google.android.exoplayer2.source.hls.HlsMediaSource import com.google.android.exoplayer2.ui.StyledPlayerControlView import com.google.android.exoplayer2.upstream.DataSource @@ -19,6 +16,7 @@ import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory import com.google.android.exoplayer2.util.Util import kotlinx.android.synthetic.main.activity_player.* import kotlinx.android.synthetic.main.player_controls.* +import java.util.concurrent.TimeUnit class PlayerActivity : AppCompatActivity() { @@ -31,6 +29,7 @@ class PlayerActivity : AppCompatActivity() { private var playWhenReady = true private var currentWindow = 0 private var playbackPosition: Long = 0 + private var remainingTime: Long = 0 private val rwdTime = 10000 private val fwdTime = 10000 @@ -98,7 +97,7 @@ class PlayerActivity : AppCompatActivity() { player = SimpleExoPlayer.Builder(this).build() dataSourceFactory = DefaultDataSourceFactory(this, Util.getUserAgent(this, "Teapod")) - controller = video_view.findViewById(R.id.exo_controller) + controller = video_view.findViewById(R.id.exo_controller) val mediaSource = HlsMediaSource.Factory(dataSourceFactory) .createMediaSource(MediaItem.fromUri(Uri.parse(streamUrl))) @@ -127,12 +126,27 @@ class PlayerActivity : AppCompatActivity() { } }) - // disable controls animation (time-bar) TODO enable and hide time to end with animation - controller.isAnimationEnabled = false - // when the player controls get hidden, hide the bars too video_view.setControllerVisibilityListener { - if (it == View.GONE) hideBars() + if (it == View.GONE) { + hideBars() + } + } + + controller.isAnimationEnabled = false // disable controls (time-bar) animation + controller.setProgressUpdateListener { _, _ -> + remainingTime = player.duration - player.currentPosition + + val hours = TimeUnit.MILLISECONDS.toHours(remainingTime) % 24 + val minutes = TimeUnit.MILLISECONDS.toMinutes(remainingTime) % 60 + val seconds = TimeUnit.MILLISECONDS.toSeconds(remainingTime) % 60 + + // if remaining time is below 60 minutes, don't show hours + exo_remaining.text = if (TimeUnit.MILLISECONDS.toMinutes(remainingTime) < 60) { + getString(R.string.time_min_sec, minutes, seconds) + } else { + getString(R.string.time_hour_min_sec, hours, minutes, seconds) + } } video_view.player = player diff --git a/app/src/main/res/layout/activity_player.xml b/app/src/main/res/layout/activity_player.xml index 0b25163..b550802 100644 --- a/app/src/main/res/layout/activity_player.xml +++ b/app/src/main/res/layout/activity_player.xml @@ -13,6 +13,7 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" + android:animateLayoutChanges="true" app:fastforward_increment="10000" app:rewind_increment="10000" app:controller_layout_id="@layout/player_controls"/> diff --git a/app/src/main/res/layout/player_controls.xml b/app/src/main/res/layout/player_controls.xml index 8e33074..65ac427 100644 --- a/app/src/main/res/layout/player_controls.xml +++ b/app/src/main/res/layout/player_controls.xml @@ -75,13 +75,13 @@ android:layout_width="0dp" android:layout_height="@dimen/exo_styled_progress_layout_height" app:layout_constraintBottom_toBottomOf="parent" - app:layout_constraintEnd_toStartOf="@+id/exo_duration" + app:layout_constraintEnd_toStartOf="@+id/exo_remaining" app:layout_constraintStart_toEndOf="@+id/exo_position" app:layout_constraintTop_toTopOf="parent" /> diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 52a896b..e9d4d4e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -48,6 +48,8 @@ You need to login before you can use Teapod. The Login-Data will be stored encrypted on your device. Could not login. Please try again. Password + %1$02d:%2$02d + %1$d:%2$02d:%3$02d org.mosad.teapod.encrypted_preferences