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

View file

@ -130,6 +130,7 @@ data class PlayerData(
val mirrorId: Int,
)
// YE, I KNOW, THIS COULD BE HANDLED A LOT BETTER
class PlayerFragment : Fragment() {
private var isCurrentlyPlaying: Boolean = false
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 playerResizeEnabled = true//settingsManager!!.getBoolean("player_resize_enabled", false)
private var doubleTapEnabled = false
private var useSystemBrightness = false
private var skipTime = 0L
private var prevDiffX = 0.0
@ -380,12 +382,36 @@ class PlayerFragment : Fragment() {
}
} else if (progressBarRightHolder != null) {
progressBarRightHolder?.alpha = 1f
if (useSystemBrightness) {
// 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
}
@ -457,6 +483,7 @@ class PlayerFragment : Fragment() {
}
}
@SuppressLint("SetTextI18n")
fun changeSkip(position: Long? = null) {
val data = localData
@ -504,6 +531,7 @@ class PlayerFragment : Fragment() {
private var hasUsedFirstRender = false
private fun releasePlayer() {
savePos()
isCurrentlyPlaying = false
val alphaAnimation = AlphaAnimation(0f, 1f)
alphaAnimation.duration = 100
@ -551,7 +579,10 @@ class PlayerFragment : Fragment() {
private fun savePos() {
if (this::exoPlayer.isInitialized) {
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() {
if (isCurrentlySkippingEp) return
releasePlayer()
isCurrentlySkippingEp = true
val copy = playerData.copy(episodeIndex = playerData.episodeIndex + 1)
playerData = copy
playbackPosition = 0
initPlayer()
}
@ -1164,6 +1197,8 @@ class PlayerFragment : Fragment() {
override fun onResume() {
super.onResume()
UIHelper.onAudioFocusEvent += ::handlePauseEvent
activity?.hideSystemUI()
activity?.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE
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() {
savePos()
@ -1181,6 +1222,8 @@ class PlayerFragment : Fragment() {
isInPlayer = false
releasePlayer()
UIHelper.onAudioFocusEvent -= ::handlePauseEvent
activity?.showSystemUI()
activity?.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER
}

View file

@ -70,6 +70,15 @@ fun ResultEpisode.getRealPosition(): Long {
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(
name: String?,
poster: String?,
@ -94,7 +103,7 @@ fun Context.buildResultEpisode(
}
fun ResultEpisode.getWatchProgress(): Float {
return position.toFloat() / duration
return getDisplayPosition().toFloat() / duration
}
class ResultFragment : Fragment() {