fix/pip background mode on ios
This commit is contained in:
1 parent
780c3cc439
commit
bf7120f479
3 files changed
+124
-24
No files matched your search
@@ -14,12 +14,24 @@ final class PictureInPictureChannel: NSObject, FlutterPlugin, AVPictureInPicture
|
||||
private var pictureInPictureController: AVPictureInPictureController?
|
||||
private var playerLayerHostView: UIView?
|
||||
private weak var currentPlayer: AVPlayer?
|
||||
private var currentPlayerId: Int?
|
||||
private var pipPossibleObservation: NSKeyValueObservation?
|
||||
private var pipPossibleTimeoutWorkItem: DispatchWorkItem?
|
||||
private var backgroundMode: String = "pictureInPicture"
|
||||
private var isVideoActive: Bool = false
|
||||
private var isVideoPlaying: Bool = false
|
||||
private var willResignActiveObserver: NSObjectProtocol?
|
||||
|
||||
private init(videoPlayerPluginLookup: @escaping () -> VideoPlayerPlugin?) {
|
||||
self.videoPlayerPluginLookup = videoPlayerPluginLookup
|
||||
super.init()
|
||||
setupLifecycleObserver()
|
||||
}
|
||||
|
||||
deinit {
|
||||
if let willResignActiveObserver {
|
||||
NotificationCenter.default.removeObserver(willResignActiveObserver)
|
||||
}
|
||||
}
|
||||
|
||||
static func register(with registrar: FlutterPluginRegistrar) {
|
||||
@@ -65,11 +77,86 @@ final class PictureInPictureChannel: NSObject, FlutterPlugin, AVPictureInPicture
|
||||
}
|
||||
pictureInPictureController.stopPictureInPicture()
|
||||
result(true)
|
||||
case "setBackgroundMode":
|
||||
let arguments = call.arguments as? [String: Any]
|
||||
if let mode = arguments?["mode"] as? String {
|
||||
backgroundMode = mode
|
||||
updateAutomaticStartSetting()
|
||||
}
|
||||
result(nil)
|
||||
case "setVideoActive":
|
||||
let arguments = call.arguments as? [String: Any]
|
||||
if let active = arguments?["active"] as? Bool {
|
||||
isVideoActive = active
|
||||
}
|
||||
|
||||
if isVideoActive,
|
||||
let playerId = arguments?["playerId"] as? Int {
|
||||
currentPlayerId = playerId
|
||||
_ = preparePictureInPictureController(playerId: playerId)
|
||||
}
|
||||
result(nil)
|
||||
case "setVideoPlaying":
|
||||
let arguments = call.arguments as? [String: Any]
|
||||
if let playing = arguments?["playing"] as? Bool {
|
||||
isVideoPlaying = playing
|
||||
}
|
||||
result(nil)
|
||||
default:
|
||||
result(FlutterMethodNotImplemented)
|
||||
}
|
||||
}
|
||||
|
||||
private func setupLifecycleObserver() {
|
||||
willResignActiveObserver = NotificationCenter.default.addObserver(
|
||||
forName: UIApplication.willResignActiveNotification,
|
||||
object: nil,
|
||||
queue: .main
|
||||
) { [weak self] _ in
|
||||
self?.startPictureInPictureOnAppLeaveIfNeeded()
|
||||
}
|
||||
}
|
||||
|
||||
private func startPictureInPictureOnAppLeaveIfNeeded() {
|
||||
guard backgroundMode == "pictureInPicture", isVideoActive, isVideoPlaying else {
|
||||
return
|
||||
}
|
||||
|
||||
if pictureInPictureController == nil,
|
||||
let currentPlayerId {
|
||||
_ = preparePictureInPictureController(playerId: currentPlayerId)
|
||||
}
|
||||
|
||||
guard let pictureInPictureController, !pictureInPictureController.isPictureInPictureActive else {
|
||||
return
|
||||
}
|
||||
|
||||
// Best effort fallback: at resign-active time iOS may still report
|
||||
// `isPictureInPicturePossible == false` briefly even though PiP can start.
|
||||
pictureInPictureController.startPictureInPicture()
|
||||
}
|
||||
|
||||
private func updateAutomaticStartSetting() {
|
||||
guard #available(iOS 14.2, *) else { return }
|
||||
pictureInPictureController?.canStartPictureInPictureAutomaticallyFromInline =
|
||||
(backgroundMode == "pictureInPicture")
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
private func preparePictureInPictureController(playerId: Int) -> Bool {
|
||||
guard let player = playerFor(playerId: playerId) else {
|
||||
return false
|
||||
}
|
||||
|
||||
currentPlayerId = playerId
|
||||
|
||||
if currentPlayer === player, pictureInPictureController != nil {
|
||||
return true
|
||||
}
|
||||
|
||||
return preparePictureInPictureController(player: player)
|
||||
}
|
||||
|
||||
private func startPictureInPicture(playerId: Int, result: @escaping FlutterResult) {
|
||||
guard AVPictureInPictureController.isPictureInPictureSupported() else {
|
||||
result(false)
|
||||
@@ -178,6 +265,7 @@ final class PictureInPictureChannel: NSObject, FlutterPlugin, AVPictureInPicture
|
||||
pictureInPictureController = controller
|
||||
playerLayerHostView = hostView
|
||||
currentPlayer = player
|
||||
updateAutomaticStartSetting()
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -191,6 +279,9 @@ final class PictureInPictureChannel: NSObject, FlutterPlugin, AVPictureInPicture
|
||||
playerLayerHostView?.removeFromSuperview()
|
||||
playerLayerHostView = nil
|
||||
currentPlayer = nil
|
||||
currentPlayerId = nil
|
||||
isVideoActive = false
|
||||
isVideoPlaying = false
|
||||
}
|
||||
|
||||
private func ensureHostView() -> UIView? {
|
||||
|
||||
@@ -87,7 +87,7 @@ class PictureInPicture {
|
||||
}
|
||||
|
||||
static Future<void> setBackgroundMode(String mode) async {
|
||||
if (!_isAndroid) return;
|
||||
if (!_isSupportedPlatform) return;
|
||||
await _channel.invokeMethod<void>('setBackgroundMode', {'mode': mode});
|
||||
}
|
||||
|
||||
@@ -98,13 +98,16 @@ class PictureInPicture {
|
||||
});
|
||||
}
|
||||
|
||||
static Future<void> setVideoActive(bool active) async {
|
||||
if (!_isAndroid) return;
|
||||
await _channel.invokeMethod<void>('setVideoActive', {'active': active});
|
||||
static Future<void> setVideoActive(bool active, {int? playerId}) async {
|
||||
if (!_isSupportedPlatform) return;
|
||||
await _channel.invokeMethod<void>('setVideoActive', {
|
||||
'active': active,
|
||||
'playerId': playerId,
|
||||
});
|
||||
}
|
||||
|
||||
static Future<void> setVideoPlaying(bool playing) async {
|
||||
if (!_isAndroid) return;
|
||||
if (!_isSupportedPlatform) return;
|
||||
await _channel.invokeMethod<void>('setVideoPlaying', {'playing': playing});
|
||||
}
|
||||
}
|
||||
@@ -191,27 +191,27 @@ class MyVideoPlayerController extends _$MyVideoPlayerController {
|
||||
void _onAppLifecycleStateChange(AppLifecycleState appState) {
|
||||
if (state == null) return;
|
||||
|
||||
final mode =
|
||||
ref.read(backgroundModeProvider) ?? BackgroundPlaybackModes.defaultMode;
|
||||
|
||||
if (appState == AppLifecycleState.inactive) {
|
||||
_wasPlayingBeforeBackground = state!.value.isPlaying;
|
||||
} else if (appState == AppLifecycleState.hidden &&
|
||||
_wasPlayingBeforeBackground) {
|
||||
final mode =
|
||||
ref.read(backgroundModeProvider) ??
|
||||
BackgroundPlaybackModes.defaultMode;
|
||||
switch (mode) {
|
||||
case BackgroundPlaybackModes.pictureInPicture:
|
||||
// On iOS the system drives PiP automatically when the app is
|
||||
// backgrounded while the PiP controller is active, so we start it
|
||||
// here. On Android PiP is handled natively via onUserLeaveHint.
|
||||
if (Platform.isIOS) {
|
||||
PictureInPicture.start(state!);
|
||||
}
|
||||
case BackgroundPlaybackModes.backgroundPlayback:
|
||||
if (Platform.isIOS) {
|
||||
_ensureBackgroundPlayback();
|
||||
}
|
||||
case BackgroundPlaybackModes.pause:
|
||||
pause();
|
||||
if (mode == BackgroundPlaybackModes.pictureInPicture) {
|
||||
// iOS PiP mode is managed on the native side during app-leave.
|
||||
return;
|
||||
}
|
||||
|
||||
if (mode == BackgroundPlaybackModes.backgroundPlayback) {
|
||||
if (Platform.isIOS) {
|
||||
_ensureBackgroundPlayback();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (mode == BackgroundPlaybackModes.pause) {
|
||||
pause();
|
||||
}
|
||||
} else if (appState == AppLifecycleState.resumed) {
|
||||
_backgroundPlayTimer?.cancel();
|
||||
@@ -397,7 +397,10 @@ class MyVideoPlayerController extends _$MyVideoPlayerController {
|
||||
}
|
||||
|
||||
PictureInPicture.setAspectRatio(videoPlayerController.value.aspectRatio);
|
||||
PictureInPicture.setVideoActive(true);
|
||||
// `playerId` is needed by native iOS PiP setup.
|
||||
// ignore: invalid_use_of_visible_for_testing_member
|
||||
final playerId = videoPlayerController.playerId;
|
||||
PictureInPicture.setVideoActive(true, playerId: playerId);
|
||||
|
||||
final handler = await ref.read(audioHandlerProvider.future);
|
||||
handler.attachController(
|
||||
@@ -465,7 +468,10 @@ class MyVideoPlayerController extends _$MyVideoPlayerController {
|
||||
}
|
||||
|
||||
PictureInPicture.setAspectRatio(videoPlayerController.value.aspectRatio);
|
||||
PictureInPicture.setVideoActive(true);
|
||||
// `playerId` is needed by native iOS PiP setup.
|
||||
// ignore: invalid_use_of_visible_for_testing_member
|
||||
final playerId = videoPlayerController.playerId;
|
||||
PictureInPicture.setVideoActive(true, playerId: playerId);
|
||||
|
||||
if (videoPlayerController.isVideoTrackSupportAvailable()) {
|
||||
try {
|
||||
|
||||
Reference in new issue
Block a user