diff --git a/playback-sdk-android/src/main/java/com/streamamg/PlaybackSDKManager.kt b/playback-sdk-android/src/main/java/com/streamamg/PlaybackSDKManager.kt index 28f7566..6f8e851 100644 --- a/playback-sdk-android/src/main/java/com/streamamg/PlaybackSDKManager.kt +++ b/playback-sdk-android/src/main/java/com/streamamg/PlaybackSDKManager.kt @@ -27,7 +27,7 @@ object PlaybackSDKManager { //region Private Properties - private lateinit var playBackAPI: PlaybackAPI + private var playBackAPI: PlaybackAPI? = null private lateinit var playerInformationAPI: PlayerInformationAPI private lateinit var amgAPIKey: String @@ -164,15 +164,15 @@ object PlaybackSDKManager { completion: (URL?, PlaybackAPIError?) -> Unit ) { coroutineScope.launch(Dispatchers.IO) { - playBackAPI.getVideoDetails(entryId, authorizationToken, userAgent) - .catch { e -> + playBackAPI?.getVideoDetails(entryId, authorizationToken, userAgent) + ?.catch { e -> // Handle the PlaybackAPIError or any other Throwable as a PlaybackAPIError when (e) { is PlaybackAPIError -> completion(null, e) else -> completion(null, PlaybackAPIError.NetworkError(e)) } } - .collect { videoDetails -> + ?.collect { videoDetails -> // Successfully retrieved video details, now check for the HLS URL val hlsURLString = videoDetails.media?.hls if (!hlsURLString.isNullOrEmpty()) { diff --git a/playback-sdk-android/src/main/java/com/streamamg/player/plugin/bitmovin/VideoPlayerViewModel.kt b/playback-sdk-android/src/main/java/com/streamamg/player/plugin/bitmovin/VideoPlayerViewModel.kt index ecc5f1b..cf131b0 100644 --- a/playback-sdk-android/src/main/java/com/streamamg/player/plugin/bitmovin/VideoPlayerViewModel.kt +++ b/playback-sdk-android/src/main/java/com/streamamg/player/plugin/bitmovin/VideoPlayerViewModel.kt @@ -11,7 +11,9 @@ import androidx.lifecycle.findViewTreeLifecycleOwner import androidx.lifecycle.viewModelScope import com.bitmovin.player.api.Player import com.bitmovin.player.api.PlayerConfig +import com.bitmovin.player.api.event.Event import com.bitmovin.player.api.event.PlayerEvent +import com.bitmovin.player.api.event.on import com.bitmovin.player.api.source.SourceConfig import com.streamamg.PlaybackSDKManager import com.streamamg.player.plugin.VideoPlayerConfig @@ -30,6 +32,7 @@ class VideoPlayerViewModel : ViewModel() { private var backgroundPlaybackEnabled = false private var autoplayEnabled = false private var isPermissionsGranted = false + private var isPlayerPaused = false private var _isPlayerReady = MutableStateFlow(false) val isPlayerReady: StateFlow get() = _isPlayerReady @@ -77,10 +80,9 @@ class VideoPlayerViewModel : ViewModel() { private fun loadVideo(videoUrl: String) { if (!urlsAreEqualExcludingKs(currentVideoUrl ?: "", videoUrl)) { val sourceConfig = SourceConfig.fromUrl(videoUrl) - Log.d("SDK", "Loading new Source") + isPlayerPaused = false player?.load(sourceConfig) } - Log.d("SDK", "Loading existing source") currentVideoUrl = videoUrl player?.next(PlayerEvent.Ready::class.java) { _isPlayerReady.value = true @@ -88,7 +90,10 @@ class VideoPlayerViewModel : ViewModel() { player?.next(PlayerEvent.Error::class.java) { Log.d("SDK", "Player error") } - if (autoplayEnabled) { + player?.next(PlayerEvent.Paused::class) { + isPlayerPaused = player?.isPaused == true + } + if (autoplayEnabled && !isPlayerPaused) { player?.play() } @@ -132,7 +137,7 @@ class VideoPlayerViewModel : ViewModel() { fun handleAppInForeground(context: Context) { if (backgroundPlaybackEnabled && _isPlayerReady.value) { unbindFromService(context) - } else if (autoplayEnabled) { + } else if (autoplayEnabled && !isPlayerPaused) { player?.play() } }