diff --git a/app/src/main/java/com/github/damontecres/wholphin/ui/playback/PlaybackViewModel.kt b/app/src/main/java/com/github/damontecres/wholphin/ui/playback/PlaybackViewModel.kt index 0cefdcdf..1951d1d9 100644 --- a/app/src/main/java/com/github/damontecres/wholphin/ui/playback/PlaybackViewModel.kt +++ b/app/src/main/java/com/github/damontecres/wholphin/ui/playback/PlaybackViewModel.kt @@ -219,18 +219,22 @@ class PlaybackViewModel PlayerBackend.PREFER_MPV -> if (isHdr) PlayerBackend.EXO_PLAYER else PlayerBackend.MPV } - Timber.i("Selected backend: %s", playerBackend) - withContext(Dispatchers.Main) { - disconnectPlayer() - } + Timber.d("Selected backend: %s", playerBackend) + if (currentPlayer.value?.backend != playerBackend) { + Timber.i("Switching player backend to %s", playerBackend) + withContext(Dispatchers.Main) { + disconnectPlayer() + } - player = - playerFactory.createVideoPlayer( - playerBackend, - preferences.appPreferences.playbackPreferences, - ) - currentPlayer.update { - PlayerState(player, playerBackend) + player = + playerFactory.createVideoPlayer( + playerBackend, + preferences.appPreferences.playbackPreferences, + ) + currentPlayer.update { + PlayerState(player, playerBackend) + } + configurePlayer() } } @@ -429,7 +433,6 @@ class PlaybackViewModel // Create the correct player for the media createPlayer(videoStream?.hdr == true) - configurePlayer() val subtitleStreams = mediaSource.mediaStreams diff --git a/app/src/main/java/com/github/damontecres/wholphin/util/mpv/MPVLib.kt b/app/src/main/java/com/github/damontecres/wholphin/util/mpv/MPVLib.kt index e5b902d7..fad62f2b 100644 --- a/app/src/main/java/com/github/damontecres/wholphin/util/mpv/MPVLib.kt +++ b/app/src/main/java/com/github/damontecres/wholphin/util/mpv/MPVLib.kt @@ -39,17 +39,9 @@ object MPVLib { external fun create(appctx: Context) - fun initialize() { - synchronized(this) { init() } - } + external fun init() - private external fun init() - - fun tearDown() { - synchronized(this) { destroy() } - } - - private external fun destroy() + external fun destroy() external fun attachSurface(surface: Surface) diff --git a/app/src/main/java/com/github/damontecres/wholphin/util/mpv/MpvPlayer.kt b/app/src/main/java/com/github/damontecres/wholphin/util/mpv/MpvPlayer.kt index 58c976c4..77613be3 100644 --- a/app/src/main/java/com/github/damontecres/wholphin/util/mpv/MpvPlayer.kt +++ b/app/src/main/java/com/github/damontecres/wholphin/util/mpv/MpvPlayer.kt @@ -74,6 +74,8 @@ class MpvPlayer( SurfaceHolder.Callback { companion object { private const val DEBUG = false + + private val initLock = Any() } private var surface: Surface? = null @@ -131,7 +133,7 @@ class MpvPlayer( MPVLib.setOptionString("demuxer-max-back-bytes", "${cacheMegs * 1024 * 1024}") Timber.v("Initializing MPVLib") - MPVLib.initialize() + MPVLib.init() MPVLib.setOptionString("force-window", "no") MPVLib.setOptionString("idle", "yes") @@ -1086,14 +1088,18 @@ class MpvPlayer( } MpvCommand.INITIALIZE -> { - init() + synchronized(initLock) { + init() + } } MpvCommand.DESTROY -> { - clearVideoSurfaceView(null) - MPVLib.removeLogObserver(mpvLogger) - MPVLib.tearDown() - Timber.d("MPVLib destroyed") + synchronized(initLock) { + MPVLib.setPropertyBoolean("pause", true) + MPVLib.removeLogObserver(mpvLogger) + MPVLib.destroy() + Timber.d("MPVLib destroyed") + } } } }