Fix offline player play/pause button

This commit is contained in:
Krunal Patel 2022-12-06 11:57:44 +05:30
parent 11d4b86129
commit 363e1a9834

View File

@ -13,6 +13,7 @@ import androidx.activity.viewModels
import androidx.core.view.WindowCompat import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat import androidx.core.view.WindowInsetsControllerCompat
import com.github.libretube.R
import com.github.libretube.constants.IntentData import com.github.libretube.constants.IntentData
import com.github.libretube.databinding.ActivityOfflinePlayerBinding import com.github.libretube.databinding.ActivityOfflinePlayerBinding
import com.github.libretube.databinding.ExoStyledPlayerControlViewBinding import com.github.libretube.databinding.ExoStyledPlayerControlViewBinding
@ -23,6 +24,7 @@ import com.github.libretube.util.DownloadHelper
import com.github.libretube.util.PlayerHelper import com.github.libretube.util.PlayerHelper
import com.google.android.exoplayer2.ExoPlayer import com.google.android.exoplayer2.ExoPlayer
import com.google.android.exoplayer2.MediaItem import com.google.android.exoplayer2.MediaItem
import com.google.android.exoplayer2.Player
import com.google.android.exoplayer2.source.MergingMediaSource import com.google.android.exoplayer2.source.MergingMediaSource
import com.google.android.exoplayer2.source.ProgressiveMediaSource import com.google.android.exoplayer2.source.ProgressiveMediaSource
import com.google.android.exoplayer2.ui.StyledPlayerView import com.google.android.exoplayer2.ui.StyledPlayerView
@ -70,6 +72,19 @@ class OfflinePlayerActivity : BaseActivity() {
playerBinding.closeImageButton.setOnClickListener { playerBinding.closeImageButton.setOnClickListener {
finish() finish()
} }
playerBinding.playPauseBTN.setOnClickListener {
if (!player.isPlaying) {
// start or go on playing
if (player.playbackState == Player.STATE_ENDED) {
// restart video if finished
player.seekTo(0)
}
player.play()
} else {
// pause the video
player.pause()
}
}
binding.player.initialize( binding.player.initialize(
null, null,
@ -77,6 +92,33 @@ class OfflinePlayerActivity : BaseActivity() {
binding.playerGestureControlsView.binding, binding.playerGestureControlsView.binding,
null null
) )
player.addListener(object : Player.Listener {
override fun onEvents(player: Player, events: Player.Events) {
super.onEvents(player, events)
if (events.containsAny(
Player.EVENT_PLAYBACK_STATE_CHANGED,
Player.EVENT_IS_PLAYING_CHANGED,
Player.EVENT_PLAY_WHEN_READY_CHANGED
)
) {
updatePlayPauseButton()
}
}
})
}
private fun updatePlayPauseButton() {
if (player.isPlaying) {
// video is playing
playerBinding.playPauseBTN.setImageResource(R.drawable.ic_pause)
} else if (player.playbackState == Player.STATE_ENDED) {
// video has finished
playerBinding.playPauseBTN.setImageResource(R.drawable.ic_restart)
} else {
// player in any other state
playerBinding.playPauseBTN.setImageResource(R.drawable.ic_play)
}
} }
private fun File.toUri(): Uri? { private fun File.toUri(): Uri? {