Skip to content

Commit

Permalink
fix(Android): cannot play on stopped player
Browse files Browse the repository at this point in the history
  • Loading branch information
hans00 committed Sep 19, 2023
1 parent bedca0d commit 931858e
Showing 1 changed file with 32 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ import com.facebook.react.uimanager.UIManagerModule
class ReactNativeVideoPlayerView : FrameLayout, SurfaceHolder.Callback, TextureView.SurfaceTextureListener, LifecycleEventListener,
OnPreparedListener, OnCompletionListener, OnErrorListener, OnInfoListener, OnSeekCompleteListener, OnVideoSizeChangedListener {

enum class State {
IDLE,
PREPARING,
PREPARED,
PLAYING,
PAUSED,
STOPPED,
COMPLETED
}

protected var mUrl: String? = null
protected var mHeaders: Map<String, String>? = null
protected var mMuted = false
Expand All @@ -44,7 +54,7 @@ class ReactNativeVideoPlayerView : FrameLayout, SurfaceHolder.Callback, TextureV
protected var mUseTextureView = false
protected val mPlaybackParams = PlaybackParams()
// State
protected var mPlaying = false
protected var mState = State.IDLE
protected var mPosition = 0L
protected var mBackgroundPaused = false

Expand Down Expand Up @@ -109,7 +119,6 @@ class ReactNativeVideoPlayerView : FrameLayout, SurfaceHolder.Callback, TextureV

private fun moveToBackground() {
mBackgroundPaused = true
mPlaying = player?.isPlaying ?: false
mPosition = player?.currentPosition?.toLong() ?: 0L
player?.pause()
}
Expand All @@ -120,7 +129,7 @@ class ReactNativeVideoPlayerView : FrameLayout, SurfaceHolder.Callback, TextureV
if (!mUseTextureView) {
player?.setDisplay((videoView as SurfaceView).holder)
}
if (mPlaying) {
if (mState == State.PLAYING) {
player?.seekTo(mPosition, MediaPlayer.SEEK_CLOSEST)
}
}
Expand Down Expand Up @@ -198,6 +207,7 @@ class ReactNativeVideoPlayerView : FrameLayout, SurfaceHolder.Callback, TextureV
player!!.setScreenOnWhilePlaying(true)
}
if (mUrl?.isEmpty() == false) {
mState = State.PREPARING
player!!.setDataSource(context, Uri.parse(mUrl), mHeaders)
player!!.prepareAsync()
player!!.setOnPreparedListener(this)
Expand All @@ -206,6 +216,8 @@ class ReactNativeVideoPlayerView : FrameLayout, SurfaceHolder.Callback, TextureV
player!!.setOnInfoListener(this)
player!!.setOnSeekCompleteListener(this)
player!!.setOnVideoSizeChangedListener(this)
} else {
mState = State.IDLE
}
}

Expand Down Expand Up @@ -311,7 +323,6 @@ class ReactNativeVideoPlayerView : FrameLayout, SurfaceHolder.Callback, TextureV
mSeekTo = (position * 1000).toLong()
return
}
mPlaying = player?.isPlaying ?: false
post {
player?.seekTo((position * 1000).toLong(), MediaPlayer.SEEK_CLOSEST)
}
Expand All @@ -322,8 +333,13 @@ class ReactNativeVideoPlayerView : FrameLayout, SurfaceHolder.Callback, TextureV
return
}
post {
player?.start()
updateProgress()
if (mState == State.STOPPED || mState == State.IDLE) {
initPlayer()
} else {
player?.start()
updateProgress()
mState = State.PLAYING
}
}
}

Expand All @@ -335,6 +351,7 @@ class ReactNativeVideoPlayerView : FrameLayout, SurfaceHolder.Callback, TextureV
player?.pause()
removeCallbacks(updateProgressTask)
updateProgress()
mState = State.PAUSED
}
}

Expand All @@ -346,6 +363,7 @@ class ReactNativeVideoPlayerView : FrameLayout, SurfaceHolder.Callback, TextureV
player?.stop()
removeCallbacks(updateProgressTask)
updateProgress()
mState = State.STOPPED
}
}

Expand All @@ -363,17 +381,22 @@ class ReactNativeVideoPlayerView : FrameLayout, SurfaceHolder.Callback, TextureV
if (mp == null) {
return
}
mState = State.PREPARED
mp.setPlaybackParams(mPlaybackParams)
mp.setLooping(mLoop)
mp.setVolume(volume, volume)
fireEvent("ready", null)
if (mSeekTo > 0) {
mPlaying = !mPaused
if (!mPaused) {
mState = State.PLAYING
}
mp.seekTo(mSeekTo, MediaPlayer.SEEK_CLOSEST)
} else {
mp.start()
if (mPaused) {
mp.pause()
} else {
mState = State.PLAYING
}
}
if (!mUseTextureView) {
Expand All @@ -384,6 +407,7 @@ class ReactNativeVideoPlayerView : FrameLayout, SurfaceHolder.Callback, TextureV
override fun onCompletion(mp: MediaPlayer?) {
removeCallbacks(updateProgressTask)
fireEvent("end", null)
mState = State.COMPLETED
}

override fun onError(mp: MediaPlayer?, what: Int, extra: Int): Boolean {
Expand Down Expand Up @@ -450,7 +474,7 @@ class ReactNativeVideoPlayerView : FrameLayout, SurfaceHolder.Callback, TextureV
}

override fun onSeekComplete(mp: MediaPlayer?) {
if (mPlaying) {
if (mState == State.PLAYING) {
updateProgress()
mp?.start()
}
Expand Down

0 comments on commit 931858e

Please sign in to comment.