fix some minor player gui issues

* hide rwd_10 button when indicator is shown
* update remaining time more often
This commit is contained in:
Jannik 2020-11-21 18:05:34 +01:00
parent 256c32aa3c
commit 91c9b6d716
Signed by: Seil0
GPG Key ID: E8459F3723C52C24
1 changed files with 61 additions and 62 deletions

View File

@ -11,10 +11,7 @@ import android.view.*
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.GestureDetectorCompat import androidx.core.view.GestureDetectorCompat
import androidx.core.view.isVisible import androidx.core.view.isVisible
import com.google.android.exoplayer2.ExoPlayer import com.google.android.exoplayer2.*
import com.google.android.exoplayer2.MediaItem
import com.google.android.exoplayer2.Player
import com.google.android.exoplayer2.SimpleExoPlayer
import com.google.android.exoplayer2.source.hls.HlsMediaSource import com.google.android.exoplayer2.source.hls.HlsMediaSource
import com.google.android.exoplayer2.ui.StyledPlayerControlView import com.google.android.exoplayer2.ui.StyledPlayerControlView
import com.google.android.exoplayer2.upstream.DataSource import com.google.android.exoplayer2.upstream.DataSource
@ -127,7 +124,6 @@ class PlayerActivity : AppCompatActivity() {
initMedia() initMedia()
initExoPlayer() initExoPlayer()
initVideoView() initVideoView()
initController()
initTimeUpdates() initTimeUpdates()
} }
@ -143,7 +139,7 @@ class PlayerActivity : AppCompatActivity() {
controller = video_view.findViewById(R.id.exo_controller) controller = video_view.findViewById(R.id.exo_controller)
val mediaSource = HlsMediaSource.Factory(dataSourceFactory) val mediaSource = HlsMediaSource.Factory(dataSourceFactory)
.createMediaSource(MediaItem.fromUri(Uri.parse(selectStream(currentEpisode)))) .createMediaSource(MediaItem.fromUri(Uri.parse(autoSelectStream(currentEpisode))))
player.playWhenReady = playWhenReady player.playWhenReady = playWhenReady
player.setMediaSource(mediaSource) player.setMediaSource(mediaSource)
@ -172,6 +168,9 @@ class PlayerActivity : AppCompatActivity() {
} }
}) })
controller.isAnimationEnabled = false // disable controls (time-bar) animation
exo_text_title.text = currentEpisode.title // set media title
} }
@SuppressLint("ClickableViewAccessibility") @SuppressLint("ClickableViewAccessibility")
@ -180,8 +179,9 @@ class PlayerActivity : AppCompatActivity() {
// when the player controls get hidden, hide the bars too // when the player controls get hidden, hide the bars too
video_view.setControllerVisibilityListener { video_view.setControllerVisibilityListener {
if (it == View.GONE) { when (it) {
hideBars() View.GONE -> hideBars()
View.VISIBLE -> updateControls()
} }
} }
@ -191,27 +191,6 @@ class PlayerActivity : AppCompatActivity() {
} }
} }
private fun initController() {
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)
}
}
exo_text_title.text = currentEpisode.title // set media title
}
private fun initActions() { private fun initActions() {
exo_close_player.setOnClickListener { this.finish() } exo_close_player.setOnClickListener { this.finish() }
rwd_10.setOnButtonClickListener { rewind() } rwd_10.setOnButtonClickListener { rewind() }
@ -224,25 +203,29 @@ class PlayerActivity : AppCompatActivity() {
timerUpdates.cancel() timerUpdates.cancel()
} }
timerUpdates = Timer().scheduleAtFixedRate(0, 1000) { timerUpdates = Timer().scheduleAtFixedRate(0, 500) {
GlobalScope.launch { GlobalScope.launch {
var btnNextEpIsVisible: Boolean var btnNextEpIsVisible: Boolean
var remainingTime: Long var controlsVisible: Boolean
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
btnNextEpIsVisible = button_next_ep.isVisible
remainingTime = player.duration - player.currentPosition remainingTime = player.duration - player.currentPosition
remainingTime = if (remainingTime < 0) 0 else remainingTime
btnNextEpIsVisible = button_next_ep.isVisible
controlsVisible = controller.isVisible
} }
if (remainingTime in 0..20000) { if (remainingTime in 1..20000 && !btnNextEpIsVisible && nextEpisode != null && Preferences.autoplay) {
if (!btnNextEpIsVisible && nextEpisode != null && Preferences.autoplay) { // if the next ep button is not visible, make it visible
// if the next ep button is not visible, make it visible withContext(Dispatchers.Main) { showButtonNextEp() }
withContext(Dispatchers.Main) { showButtonNextEp() } } else if (btnNextEpIsVisible) {
} withContext(Dispatchers.Main) { hideButtonNextEp() }
} else { }
if (btnNextEpIsVisible) {
withContext(Dispatchers.Main) { hideButtonNextEp() } // if controls are visible, update them
} if (controlsVisible) {
withContext(Dispatchers.Main) { updateControls() }
} }
} }
} }
@ -258,6 +241,23 @@ class PlayerActivity : AppCompatActivity() {
Log.d(javaClass.name, "Released player") Log.d(javaClass.name, "Released player")
} }
/**
* update the custom controls
*/
private fun updateControls() {
// update remaining time label
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)
}
}
/** /**
* TODO set position of rewind/fast forward indicators programmatically * TODO set position of rewind/fast forward indicators programmatically
*/ */
@ -268,12 +268,12 @@ class PlayerActivity : AppCompatActivity() {
// hide/show needed components // hide/show needed components
exo_double_tap_indicator.visibility = View.VISIBLE exo_double_tap_indicator.visibility = View.VISIBLE
ffwd_10_indicator.visibility = View.INVISIBLE ffwd_10_indicator.visibility = View.INVISIBLE
ffwd_10.visibility = View.INVISIBLE rwd_10.visibility = View.INVISIBLE
rwd_10_indicator.onAnimationEndCallback = { rwd_10_indicator.onAnimationEndCallback = {
exo_double_tap_indicator.visibility = View.GONE exo_double_tap_indicator.visibility = View.GONE
ffwd_10_indicator.visibility = View.VISIBLE ffwd_10_indicator.visibility = View.VISIBLE
ffwd_10.visibility = View.VISIBLE rwd_10.visibility = View.VISIBLE
} }
// run animation // run animation
@ -306,27 +306,23 @@ class PlayerActivity : AppCompatActivity() {
} }
} }
private fun playNextEpisode() { private fun playNextEpisode() = nextEpisode?.let { nextEp ->
nextEpisode?.let { nextEp -> // update the gui
exo_text_title.text = nextEp.title
hideButtonNextEp()
player.clearMediaItems() //remove previous item
val mediaSource = HlsMediaSource.Factory(dataSourceFactory)
.createMediaSource(MediaItem.fromUri(Uri.parse(autoSelectStream(nextEp))))
player.setMediaSource(mediaSource)
player.prepare()
// update the gui // watchedCallback for next ep
exo_text_title.text = nextEp.title currentEpisode = nextEp // set current ep to next ep
hideButtonNextEp() episodeId = nextEp.id
MediaFragment.instance.updateWatchedState(nextEp)
player.clearMediaItems() //remove previous item nextEpisode = selectNextEpisode()
val mediaSource = HlsMediaSource.Factory(dataSourceFactory)
.createMediaSource(MediaItem.fromUri(Uri.parse(selectStream(nextEp))))
player.setMediaSource(mediaSource)
player.prepare()
// watchedCallback for next ep
currentEpisode = nextEp // set current ep to next ep
episodeId = nextEp.id
MediaFragment.instance.updateWatchedState(nextEp)
nextEpisode = selectNextEpisode()
}
} }
/** /**
@ -334,7 +330,7 @@ class PlayerActivity : AppCompatActivity() {
* use the secondary stream. Else, if the primary stream is set use the primary stream. * use the secondary stream. Else, if the primary stream is set use the primary stream.
* If no stream is present, close the activity. * If no stream is present, close the activity.
*/ */
private fun selectStream(episode: Episode): String { private fun autoSelectStream(episode: Episode): String {
return if ((Preferences.preferSecondary || episode.priStreamUrl.isEmpty()) && episode.secStreamOmU) { return if ((Preferences.preferSecondary || episode.priStreamUrl.isEmpty()) && episode.secStreamOmU) {
episode.secStreamUrl episode.secStreamUrl
} else if (episode.priStreamUrl.isNotEmpty()) { } else if (episode.priStreamUrl.isNotEmpty()) {
@ -445,6 +441,9 @@ class PlayerActivity : AppCompatActivity() {
return true return true
} }
/**
* on long press toggle pause/play
*/
override fun onLongPress(e: MotionEvent?) { override fun onLongPress(e: MotionEvent?) {
togglePausePlay() togglePausePlay()
} }