Move duplicate code of playPauseBtn to CustomExoPlayerView

This commit is contained in:
Krunal Patel 2022-12-06 15:07:08 +05:30
parent 363e1a9834
commit bbd9b8546a
3 changed files with 42 additions and 48 deletions

View File

@ -13,7 +13,6 @@ import androidx.activity.viewModels
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
import com.github.libretube.R
import com.github.libretube.constants.IntentData
import com.github.libretube.databinding.ActivityOfflinePlayerBinding
import com.github.libretube.databinding.ExoStyledPlayerControlViewBinding
@ -24,7 +23,6 @@ import com.github.libretube.util.DownloadHelper
import com.github.libretube.util.PlayerHelper
import com.google.android.exoplayer2.ExoPlayer
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.ProgressiveMediaSource
import com.google.android.exoplayer2.ui.StyledPlayerView
@ -72,19 +70,6 @@ class OfflinePlayerActivity : BaseActivity() {
playerBinding.closeImageButton.setOnClickListener {
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(
null,
@ -92,33 +77,6 @@ class OfflinePlayerActivity : BaseActivity() {
binding.playerGestureControlsView.binding,
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? {

View File

@ -317,7 +317,7 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
BackgroundHelper.stopBackgroundPlay(requireContext())
}
val playPauseClickListner = View.OnClickListener {
binding.playImageView.setOnClickListener {
if (!exoPlayer.isPlaying) {
// start or go on playing
if (exoPlayer.playbackState == Player.STATE_ENDED) {
@ -330,8 +330,6 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
exoPlayer.pause()
}
}
playerBinding.playPauseBTN.setOnClickListener(playPauseClickListner)
binding.playImageView.setOnClickListener(playPauseClickListner)
// video description and chapters toggle
binding.playerTitleLayout.setOnClickListener {
@ -976,15 +974,12 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
if (exoPlayer.isPlaying) {
// video is playing
binding.playImageView.setImageResource(R.drawable.ic_pause)
playerBinding.playPauseBTN.setImageResource(R.drawable.ic_pause)
} else if (exoPlayer.playbackState == Player.STATE_ENDED) {
// video has finished
binding.playImageView.setImageResource(R.drawable.ic_restart)
playerBinding.playPauseBTN.setImageResource(R.drawable.ic_restart)
} else {
// player in any other state
binding.playImageView.setImageResource(R.drawable.ic_play)
playerBinding.playPauseBTN.setImageResource(R.drawable.ic_play)
}
}

View File

@ -125,6 +125,47 @@ internal class CustomExoPlayerView(
"zoom" -> AspectRatioFrameLayout.RESIZE_MODE_ZOOM
else -> AspectRatioFrameLayout.RESIZE_MODE_FIT
}
binding.playPauseBTN.setOnClickListener {
if (player?.isPlaying == false) {
// 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()
}
}
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 == true) {
// video is playing
binding.playPauseBTN.setImageResource(R.drawable.ic_pause)
} else if (player?.playbackState == Player.STATE_ENDED) {
// video has finished
binding.playPauseBTN.setImageResource(R.drawable.ic_restart)
} else {
// player in any other state
binding.playPauseBTN.setImageResource(R.drawable.ic_play)
}
}
override fun hideController() {