Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Initializing player in every onResume() call #108

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions player/src/main/java/com/tpstream/player/TpStreamPlayer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ internal class TpStreamPlayerImpl(val context: Context) : TpStreamPlayer {
exoPlayer.setMediaSource(getMediaSourceFactory().createMediaSource(getMediaItem(url)))
exoPlayer.seekTo(startPosition)
exoPlayer.prepare()
tpStreamPlayerImplCallBack?.onPlayerPrepare()
}

private fun getMediaSourceFactory(): MediaSource.Factory {
Expand Down Expand Up @@ -212,4 +213,5 @@ internal class TpStreamPlayerImpl(val context: Context) : TpStreamPlayer {
internal interface TpStreamPlayerImplCallBack {

fun updateDownloadButton(showDownloadButton: Boolean,videoId: String)
fun onPlayerPrepare()
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import kotlinx.coroutines.launch

class TpStreamPlayerFragment : Fragment(), DownloadCallback.Listener {
private val _playbackStateListener: Player.Listener = PlayerListener()
private lateinit var player: TpStreamPlayerImpl
private var player: TpStreamPlayerImpl? = null
private var _viewBinding: FragmentTpStreamPlayerBinding? = null
private val viewBinding get() = _viewBinding!!
private val TAG = "TpStreamPlayerFragment"
Expand Down Expand Up @@ -92,33 +92,56 @@ class TpStreamPlayerFragment : Fragment(), DownloadCallback.Listener {
super.onStart()
if (Util.SDK_INT > 23) {
initializePlayer()
if (viewBinding.videoView != null) {
viewBinding.videoView.onResume()
}
}
}

override fun onResume() {
super.onResume()
if (Util.SDK_INT <= 23 || player == null) {
initializePlayer()
if (viewBinding.videoView != null) {
viewBinding.videoView.onResume()
}
}
hideSystemUi()
}

override fun onPause() {
super.onPause()
if (Util.SDK_INT <= 23) {
storeCurrentPlayTime()
player?.release()
if (viewBinding.videoView != null) {
viewBinding.videoView.onPause()
}
releasePlayer()
}
Log.d(TAG, "onPause: ")
}

override fun onStop() {
super.onStop()
if (Util.SDK_INT > 23) {
storeCurrentPlayTime()
if (viewBinding.videoView != null) {
viewBinding.videoView.onPause()
}
releasePlayer()
}
}

private fun releasePlayer() {
if (player != null) {
updateStartPosition()
player?.release()
player = null
viewBinding.videoView.player = null
}
}

private fun updateStartPosition() {
if (player != null){
startPosition = player?.getCurrentTime()?: -1L
}
Log.d(TAG, "onStop: ")
}

override fun onDestroyView() {
Expand Down Expand Up @@ -305,11 +328,13 @@ class TpStreamPlayerFragment : Fragment(), DownloadCallback.Listener {
}

private fun initializePlayer() {
player = TpStreamPlayerImpl(requireContext())
player.setTpStreamPlayerImplCallBack(tpStreamPlayerImplCallBack)
viewBinding.videoView.player = player.exoPlayer
player.exoPlayer.addListener(_playbackStateListener)
this.initializationListener?.onInitializationSuccess(player!!)
if (player == null) {
player = TpStreamPlayerImpl(requireContext())
player?.setTpStreamPlayerImplCallBack(tpStreamPlayerImplCallBack)
viewBinding.videoView.player = player?.exoPlayer
player?.exoPlayer?.addListener(_playbackStateListener)
this.initializationListener?.onInitializationSuccess(player!!)
}
}

fun setOnInitializationListener(listener: InitializationListener) {
Expand All @@ -320,10 +345,6 @@ class TpStreamPlayerFragment : Fragment(), DownloadCallback.Listener {
if (player == null) {
throw Exception("Player is not initialized yet. `load` method should be called onInitializationSuccess")
}
if (startPosition != -1L){
parameters.startAt = startPosition
}

player?.load(parameters) { exception ->
requireActivity().runOnUiThread {
showErrorMessage("Error Occurred while playing video. Error code ${exception.errorMessage}.\n ID: ${parameters.videoId}")
Expand All @@ -345,10 +366,6 @@ class TpStreamPlayerFragment : Fragment(), DownloadCallback.Listener {
tpImp.playVideo(player?.video?.url!!,currentPosition!!)
}

private fun storeCurrentPlayTime(){
startPosition = player?.getCurrentTime()?.div(1000L) ?: -1L
}

fun enableAutoFullScreenOnRotate() {
orientationEventListener = OrientationListener(requireContext())
orientationEventListener.setOnChangeListener { isLandscape ->
Expand Down Expand Up @@ -468,6 +485,13 @@ class TpStreamPlayerFragment : Fragment(), DownloadCallback.Listener {
}
}

override fun onPlayerPrepare() {
if (player != null && startPosition != -1L) {
player?.seekTo(startPosition)
player?.pause()
}
}

}

private fun updateDownloadButtonImage(videoId: String){
Expand Down