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

Add missing PlayerView lifecycle handling (PiP #1) #85

Merged
merged 7 commits into from
Dec 22, 2023
Merged
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/),
and this project adheres to [Semantic Versioning](https://semver.org/).

## [Unreleased]
### Fixed

- Android: Playback doesn't pause when app goes to background

## [0.4.0] - 2023-12-12
### Added
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.bitmovin.player.flutter

import android.app.Activity
import android.content.Context
import android.content.ContextWrapper
import android.view.View
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import com.bitmovin.player.PlayerView
import com.bitmovin.player.api.Player
import com.bitmovin.player.flutter.json.JPlayerViewCreateArgs
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.EventChannel
Expand All @@ -22,7 +25,6 @@ class FlutterPlayerView(
id: Int,
args: Any?,
) : MethodChannel.MethodCallHandler, EventChannel.StreamHandler, PlatformView, EventListener() {
private val playerView: PlayerView
private val methodChannel: MethodChannel =
MethodChannel(
messenger,
Expand All @@ -35,10 +37,33 @@ class FlutterPlayerView(
messenger,
)

private val playerView: PlayerView = PlayerView(context, player = null)

private val activity =
context.getActivity()
?: error("Trying to create an instance of ${this::class.simpleName} while not attached to an Activity")

private val activityLifecycle =
(activity as? LifecycleOwner)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The activity should always be null or a LifecycleOwner, right?

Suggested change
(activity as? LifecycleOwner)
(activity as LifecycleOwner?)

Copy link
Contributor Author

@zigavehovec zigavehovec Dec 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The activity should never be null at this point, but it might not extend LifecycleOwner.

?.lifecycle
?: error("Trying to create an instance of ${this::class.simpleName} while not attached to a LifecycleOwner")

private val activityLifecycleObserver =
object : DefaultLifecycleObserver {
override fun onStart(owner: LifecycleOwner) = playerView.onStart()

override fun onResume(owner: LifecycleOwner) = playerView.onResume()

override fun onPause(owner: LifecycleOwner) = playerView.onPause()

override fun onStop(owner: LifecycleOwner) = playerView.onStop()

override fun onDestroy(owner: LifecycleOwner) = dispose()
}

init {
val playerViewCreateArgs = JPlayerViewCreateArgs(args as Map<*, *>)

playerView = PlayerView(context, null as Player?)
PlayerManager.onPlayerCreated(playerViewCreateArgs.playerId) { player ->
playerView.player = player
if (playerViewCreateArgs.hasFullscreenHandler) {
Expand All @@ -50,6 +75,8 @@ class FlutterPlayerView(
)
}
}

activityLifecycle.addObserver(activityLifecycleObserver)
}

override fun onMethodCall(
Expand All @@ -58,7 +85,7 @@ class FlutterPlayerView(
) = when (call.method) {
Methods.ENTER_FULLSCREEN -> playerView.enterFullscreen()
Methods.EXIT_FULLSCREEN -> playerView.exitFullscreen()
Methods.DESTROY_PLAYER_VIEW -> { /* no-op */ }
Methods.DESTROY_PLAYER_VIEW -> Unit // no-op
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, I like this style more 👍

else -> throw NotImplementedError()
}

Expand All @@ -69,6 +96,7 @@ class FlutterPlayerView(
playerView.onDestroy()
methodChannel.setMethodCallHandler(null)
eventChannel.setStreamHandler(null)
activityLifecycle.removeObserver(activityLifecycleObserver)
}

override fun onListen(
Expand All @@ -82,4 +110,11 @@ class FlutterPlayerView(
override fun onCancel(arguments: Any?) {
sink = null
}

private fun Context.getActivity(): Activity? =
when (this) {
is Activity -> this
is ContextWrapper -> baseContext.getActivity()
else -> null
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throw instead of returning null? this could even be inlined.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went for nullable approach to be able to provide a more descriptive error message where it's used (FlutterPlayerView.kt:46).
Since we might also need it somewhere else I decided to just leave it generic, so that it can just be extracted in that case.

}
}