Skip to content

Commit

Permalink
[CORE-4969] Fixed crash with lateinit. Fixed play/pause state
Browse files Browse the repository at this point in the history
  • Loading branch information
KharchenkoAlex committed Oct 23, 2024
1 parent e2c9ca5 commit 619c1e1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<Boolean>
get() = _isPlayerReady
Expand Down Expand Up @@ -77,18 +80,20 @@ 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
}
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()
}

Expand Down Expand Up @@ -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()
}
}
Expand Down

0 comments on commit 619c1e1

Please sign in to comment.