player bug

This commit is contained in:
LagradOst 2021-06-16 19:40:02 +02:00
parent 5938d4824d
commit 9550154a49
3 changed files with 71 additions and 19 deletions

View file

@ -24,7 +24,6 @@ import androidx.appcompat.view.menu.MenuBuilder
import androidx.appcompat.widget.PopupMenu import androidx.appcompat.widget.PopupMenu
import androidx.core.app.ActivityCompat import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.core.view.forEach
import androidx.fragment.app.FragmentActivity import androidx.fragment.app.FragmentActivity
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import com.google.android.gms.cast.framework.CastContext import com.google.android.gms.cast.framework.CastContext
@ -119,23 +118,23 @@ object UIHelper {
) )
} }
} }
private var _AudioFocusRequest: AudioFocusRequest? = null private var currentAudioFocusRequest: AudioFocusRequest? = null
private var _OnAudioFocusChangeListener: AudioManager.OnAudioFocusChangeListener? = null private var currentAudioFocusChangeListener: AudioManager.OnAudioFocusChangeListener? = null
var onAudioFocusEvent = Event<Boolean>() var onAudioFocusEvent = Event<Boolean>()
private fun getAudioListener(): AudioManager.OnAudioFocusChangeListener? { private fun getAudioListener(): AudioManager.OnAudioFocusChangeListener? {
if (_OnAudioFocusChangeListener != null) return _OnAudioFocusChangeListener if (currentAudioFocusChangeListener != null) return currentAudioFocusChangeListener
_OnAudioFocusChangeListener = AudioManager.OnAudioFocusChangeListener { currentAudioFocusChangeListener = AudioManager.OnAudioFocusChangeListener {
onAudioFocusEvent.invoke( onAudioFocusEvent.invoke(
when (it) { when (it) {
AudioManager.AUDIOFOCUS_GAIN -> true AudioManager.AUDIOFOCUS_GAIN -> false
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE -> true AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE -> false
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT -> true AudioManager.AUDIOFOCUS_GAIN_TRANSIENT -> false
else -> false else -> true
} }
) )
} }
return _OnAudioFocusChangeListener return currentAudioFocusChangeListener
} }
fun Context.isCastApiAvailable(): Boolean { fun Context.isCastApiAvailable(): Boolean {
@ -168,8 +167,8 @@ object UIHelper {
} }
fun getFocusRequest(): AudioFocusRequest? { fun getFocusRequest(): AudioFocusRequest? {
if (_AudioFocusRequest != null) return _AudioFocusRequest if (currentAudioFocusRequest != null) return currentAudioFocusRequest
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { currentAudioFocusRequest = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN).run { AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN).run {
setAudioAttributes(AudioAttributes.Builder().run { setAudioAttributes(AudioAttributes.Builder().run {
setUsage(AudioAttributes.USAGE_MEDIA) setUsage(AudioAttributes.USAGE_MEDIA)
@ -185,6 +184,7 @@ object UIHelper {
} else { } else {
null null
} }
return currentAudioFocusRequest
} }
fun Activity.hideSystemUI() { fun Activity.hideSystemUI() {

View file

@ -130,6 +130,7 @@ data class PlayerData(
val mirrorId: Int, val mirrorId: Int,
) )
// YE, I KNOW, THIS COULD BE HANDLED A LOT BETTER
class PlayerFragment : Fragment() { class PlayerFragment : Fragment() {
private var isCurrentlyPlaying: Boolean = false private var isCurrentlyPlaying: Boolean = false
private val mapper = JsonMapper.builder().addModule(KotlinModule()) private val mapper = JsonMapper.builder().addModule(KotlinModule())
@ -294,6 +295,7 @@ class PlayerFragment : Fragment() {
private var playBackSpeedEnabled = true//settingsManager!!.getBoolean("playback_speed_enabled", false) private var playBackSpeedEnabled = true//settingsManager!!.getBoolean("playback_speed_enabled", false)
private var playerResizeEnabled = true//settingsManager!!.getBoolean("player_resize_enabled", false) private var playerResizeEnabled = true//settingsManager!!.getBoolean("player_resize_enabled", false)
private var doubleTapEnabled = false private var doubleTapEnabled = false
private var useSystemBrightness = false
private var skipTime = 0L private var skipTime = 0L
private var prevDiffX = 0.0 private var prevDiffX = 0.0
@ -380,12 +382,36 @@ class PlayerFragment : Fragment() {
} }
} else if (progressBarRightHolder != null) { } else if (progressBarRightHolder != null) {
progressBarRightHolder?.alpha = 1f progressBarRightHolder?.alpha = 1f
val alpha = minOf(0.95f,
brightness_overlay.alpha + diffY.toFloat() * 0.5f) // 0.05f *if (diffY > 0) 1 else -1
brightness_overlay?.alpha = alpha
progressBarRight?.max = 100 * 100 if (useSystemBrightness) {
progressBarRight?.progress = ((1f - alpha) * 100 * 100).toInt() // https://developer.android.com/reference/android/view/WindowManager.LayoutParams#screenBrightness
val lp = activity?.window?.attributes
val currentBrightness = if (lp?.screenBrightness ?: -1.0f <= 0f) (android.provider.Settings.System.getInt(
context?.contentResolver,
android.provider.Settings.System.SCREEN_BRIGHTNESS
) * (1 / 255).toFloat())
else lp?.screenBrightness!!
val alpha = minOf(
maxOf(
0.005f, // BRIGHTNESS_OVERRIDE_OFF doesn't seem to work
currentBrightness - diffY.toFloat() * 0.5f
), 1.0f
)// 0.05f *if (diffY > 0) 1 else -1
lp?.screenBrightness = alpha
activity?.window?.attributes = lp
progressBarRight?.max = 100 * 100
progressBarRight?.progress = (alpha * 100 * 100).toInt()
}
else {
val alpha = minOf(0.95f,
brightness_overlay.alpha + diffY.toFloat() * 0.5f) // 0.05f *if (diffY > 0) 1 else -1
brightness_overlay?.alpha = alpha
progressBarRight?.max = 100 * 100
progressBarRight?.progress = ((1f - alpha) * 100 * 100).toInt()
}
currentY = motionEvent.rawY currentY = motionEvent.rawY
} }
@ -457,6 +483,7 @@ class PlayerFragment : Fragment() {
} }
} }
@SuppressLint("SetTextI18n")
fun changeSkip(position: Long? = null) { fun changeSkip(position: Long? = null) {
val data = localData val data = localData
@ -504,6 +531,7 @@ class PlayerFragment : Fragment() {
private var hasUsedFirstRender = false private var hasUsedFirstRender = false
private fun releasePlayer() { private fun releasePlayer() {
savePos()
isCurrentlyPlaying = false isCurrentlyPlaying = false
val alphaAnimation = AlphaAnimation(0f, 1f) val alphaAnimation = AlphaAnimation(0f, 1f)
alphaAnimation.duration = 100 alphaAnimation.duration = 100
@ -551,7 +579,10 @@ class PlayerFragment : Fragment() {
private fun savePos() { private fun savePos() {
if (this::exoPlayer.isInitialized) { if (this::exoPlayer.isInitialized) {
if (exoPlayer.duration > 0 && exoPlayer.currentPosition > 0) { if (exoPlayer.duration > 0 && exoPlayer.currentPosition > 0) {
context?.setViewPos(getEpisode()?.id, exoPlayer.currentPosition, exoPlayer.duration) context?.let { ctx ->
ctx.setViewPos(getEpisode()?.id, exoPlayer.currentPosition, exoPlayer.duration)
viewModel.reloadEpisodes(ctx)
}
} }
} }
} }
@ -1142,9 +1173,11 @@ class PlayerFragment : Fragment() {
private fun skipToNextEpisode() { private fun skipToNextEpisode() {
if (isCurrentlySkippingEp) return if (isCurrentlySkippingEp) return
releasePlayer()
isCurrentlySkippingEp = true isCurrentlySkippingEp = true
val copy = playerData.copy(episodeIndex = playerData.episodeIndex + 1) val copy = playerData.copy(episodeIndex = playerData.episodeIndex + 1)
playerData = copy playerData = copy
playbackPosition = 0
initPlayer() initPlayer()
} }
@ -1164,6 +1197,8 @@ class PlayerFragment : Fragment() {
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
UIHelper.onAudioFocusEvent += ::handlePauseEvent
activity?.hideSystemUI() activity?.hideSystemUI()
activity?.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE activity?.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE
if (Util.SDK_INT <= 23) { if (Util.SDK_INT <= 23) {
@ -1174,6 +1209,12 @@ class PlayerFragment : Fragment() {
} }
} }
private fun handlePauseEvent(pause : Boolean) {
if(pause) {
handlePlayerEvent(PlayerEventType.Pause)
}
}
override fun onDestroy() { override fun onDestroy() {
savePos() savePos()
@ -1181,6 +1222,8 @@ class PlayerFragment : Fragment() {
isInPlayer = false isInPlayer = false
releasePlayer() releasePlayer()
UIHelper.onAudioFocusEvent -= ::handlePauseEvent
activity?.showSystemUI() activity?.showSystemUI()
activity?.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER activity?.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER
} }

View file

@ -70,6 +70,15 @@ fun ResultEpisode.getRealPosition(): Long {
return position return position
} }
fun ResultEpisode.getDisplayPosition(): Long {
if (duration <= 0) return 0
val percentage = position * 100 / duration
if (percentage <= 1) return 0
if (percentage <= 5) return 5 * duration / 100
if (percentage >= 95) return duration
return position
}
fun Context.buildResultEpisode( fun Context.buildResultEpisode(
name: String?, name: String?,
poster: String?, poster: String?,
@ -94,7 +103,7 @@ fun Context.buildResultEpisode(
} }
fun ResultEpisode.getWatchProgress(): Float { fun ResultEpisode.getWatchProgress(): Float {
return position.toFloat() / duration return getDisplayPosition().toFloat() / duration
} }
class ResultFragment : Fragment() { class ResultFragment : Fragment() {