Repository navigation
feat: add Picture-in-Picture support for iOS/Android video player - #432
akaboshinit wants to merge 47 commits into
Conversation
Add PiP API to platform interface (enable/disable/start/stop/isSupported/isActive) and implement it in the AVFoundation plugin with native iOS AVPictureInPictureController. Includes event handling for pipStarted, pipStopped, and pipRestoreUserInterface.
- Add PiP API to video_player_android (start, stop, isSupported, isActive) - Add VideoPlayerCallbacks interface and VideoPlayerEventCallbacks for PiP events - Implement ActivityAware in VideoPlayerPlugin for Activity lifecycle management - Add PipRequestHandler pattern to track which player requested PiP - Add isPipActive to VideoPlayerValue in video_player package - Handle pipStarted/pipStopped events in VideoPlayerController - Update PiP method docs to reflect iOS and Android support - Add PiP controls UI to both example apps - Configure AndroidManifest.xml with FlutterFragmentActivity and PiP support - Upgrade Gradle/AGP for video_player_android example (Java 17 compatibility)
When PiP starts on iOS, the app transitions to the background and _VideoAppLifeCycleObserver fires a pause. This fix skips the pause when isPipActive is true, keeping the video playing in the PiP window. Also guards resumed state to avoid auto-play when returning from PiP.
Add setAutoPictureInPicture API across platform interface, Android, and iOS. - Android 12+ (API 31+): uses native setAutoEnterEnabled - Android 8-11 (API 26-30): Flutter-side fallback via AppLifecycleState - iOS 14.2+: uses canStartPictureInPictureAutomaticallyFromInline
… to declarative Gradle plugins
- iOS: Add weak self pattern in dispatch_async blocks to prevent crashes during dispose - iOS: Release pipRestoreCompletionHandler in tearDownPictureInPicture to prevent leaks - iOS: Wrap frame changes in CATransaction to suppress implicit animations - iOS: Synchronize hidden flag instead of async dispatch - iOS: Set explicit initial frame (CGRectZero) on pipPlayerLayer - iOS: Remove unused savedFrame variable - Android: Reset lastPipPlayerId on player dispose to prevent event delivery to deleted players - Android: Remove existing PiP listener before re-registering to prevent double registration on config changes - Dart: Clear pipSourceRectProvider in controller dispose
…example UI - Defer PiP controller setup to startPictureInPicture/setAutoPictureInPicture to avoid multiple AVPictureInPictureControllers causing isPictureInPicturePossible=NO - Add exclusive auto PiP on Android: enabling on one player disables others - Remove PiP-specific fullscreen UI from example app - Clean up imports and fix code formatting
restoreUserInterface で黒オーバーレイを追加し、Flutter テクスチャの 遷移アーティファクトを隠す。didStopPictureInPicture で 0.3s フェード アウトして削除。PiP ウィンドウはシステムウィンドウのため オーバーレイの上に正常に描画される。
There was a problem hiding this comment.
PiPのときにSceneDelegateとの兼ね合いが気になります!
コアでそもそもUIScreen.mainを使っている箇所があるんですが、それがDeprecatedにもなっております。
- startPictureInPicture / isPictureInPictureSupported / isPictureInPictureActive を全層 (Dart / Java / ObjC / pigeon / test) から削除 - pipRestoreUserInterface event / completePipRestoreWithSourceRect / PipSourceRectMessage / pipSourceRectProvider / _getSourceRect を削除 - onAutoPipChanged / onPipRestoreUserInterface コールバックを削除 - Android 8-11 の auto PiP fallback ロジックも削除
- AVPlayerLayer を hidden にして PiP window のコンテンツを空にし、 iOS 内部の縮小アニメを視覚的に消す - 黒オーバーレイで Flutter view の遷移アーティファクトを補助的に隠蔽 - didStopPictureInPicture で overlay をフェードアウト削除、layer を復元 - AVPlayerLayer.frame = CGRectZero で縮小先を無効化
| // Auto PiP requires the AVPlayerLayer to have a non-zero frame so that | ||
| // iOS considers the player to be "playing inline". With CGRectZero the | ||
| // system never triggers automatic PiP on app backgrounding. | ||
| [CATransaction begin]; |
There was a problem hiding this comment.
CATransaction dispose不要?
| // system never triggers automatic PiP on app backgrounding. | ||
| [CATransaction begin]; | ||
| [CATransaction setDisableActions:YES]; | ||
| _pipPlayerLayer.frame = CGRectMake(0, 0, 1, 1); |
| return; | ||
| } | ||
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { | ||
| PictureInPictureParams params = |
There was a problem hiding this comment.
399-395 の実装とBool値以外同じ内容ではあるので、統一してもいいですね
applyPipParams(boolean) 的な
There was a problem hiding this comment.
private void applyPipParams(boolean autoEnter) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S || activity == null) {
return;
}
PictureInPictureParams params =
new PictureInPictureParams.Builder()
.setAspectRatio(getVideoAspectRatio())
.setAutoEnterEnabled(autoEnter)
.build();
try {
activity.setPictureInPictureParams(params);
} catch (IllegalStateException | IllegalArgumentException e) {
Log.w(TAG, "Failed to apply PiP params", e);
}
}
こういう
| } | ||
|
|
||
| void dispose() { | ||
| activity = null; |
There was a problem hiding this comment.
pipのenableを更新した方が良さそう
| if (videoFormat != null && videoFormat.width > 0 && videoFormat.height > 0) { | ||
| return new Rational(videoFormat.width, videoFormat.height); | ||
| } | ||
| return new Rational(16, 9); |
There was a problem hiding this comment.
決め打ちなのはpackageとしてよくないね
| @Nullable private Activity activity; | ||
| // Tracks which player last requested PiP, so events go to the right player. | ||
| private long lastPipPlayerId = -1; | ||
| @Nullable private Consumer<PictureInPictureModeChangedInfo> pipModeChangedListener; |
There was a problem hiding this comment.
メモリリーク対策として、disposeでちゃんとリセットしてあげる
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O | ||
| && boundActivity instanceof OnPictureInPictureModeChangedProvider) { | ||
| OnPictureInPictureModeChangedProvider provider = | ||
| (OnPictureInPictureModeChangedProvider) boundActivity; |
There was a problem hiding this comment.
disposeとか綺麗に書ければ、ここもスッキリするかも?
|
|
||
| @SuppressWarnings("SuspiciousNameCombination") | ||
| @VisibleForTesting | ||
| void sendInitialized() { |
There was a problem hiding this comment.
aspectratioの再計算あった方がただしそう
There was a problem hiding this comment.
-
pipの機能を使えるか確認するメソッド防御的に追加したい
- 使えない場合クラッシュする
-
android go危ないかも
-
スマホを切って戻ってきた大丈夫大丈夫か
-
reattachの時大丈夫か
- reattachされたら壊れるかも?
- クラッシュするかも?
- pipの状態がずれたりするかも
- setActivityで動画を同期やispipなどを確認
|
|
||
| if (_pipController) { | ||
| if ([_pipController isPictureInPictureActive]) { | ||
| [_pipController stopPictureInPicture]; |
There was a problem hiding this comment.
stopPictureInPicture は非同期だから、すぐにその後 delegate = nil したら飛ばない可能性があります
| _pipPlayerLayer.frame = CGRectMake(0, 0, 1, 1); | ||
| [CATransaction commit]; | ||
| _pipController.canStartPictureInPictureAutomaticallyFromInline = YES; | ||
| } |
There was a problem hiding this comment.
このelseのタイミングでpipのセットアップが失敗した時のログは送らないんですね
…ception fix: catch IllegalStateException from enterPictureInPictureMode
masksToBounds によるクリップで、復帰アニメーション中に左上が欠けて見えるため削除。
Summary
VideoPlayerControllerからstartPictureInPicture()/stopPictureInPicture()で PiP を制御可能VideoPlayerValue.isPipActiveで PiP 状態を監視可能Changes
video_player_platform_interface
startPictureInPicture,stopPictureInPicture,isPictureInPictureSupported,isPictureInPictureActive) を追加VideoEventType.pipStarted,pipStopped,pipRestoreUserInterfaceを追加video_player_avfoundation (iOS)
video_player_android (Android)
ActivityAwareを実装し Activity ライフサイクルを管理PipRequestHandlerパターンで PiP 要求元プレーヤーを追跡VideoPlayerCallbacks/VideoPlayerEventCallbacksで PiP イベントを Dart 側に通知PipStatusMessageと 4 つの PiP API を追加video_player
VideoPlayerValueにisPipActiveフィールドを追加pipStarted/pipStoppedイベントでisPipActiveを自動更新Example apps
AndroidManifest.xmlにFlutterFragmentActivity+supportsPictureInPictureを設定Usage
Android 利用側の設定
AndroidManifest.xmlの Activity に以下を追加:Test plan
isPictureInPictureSupportedが正しい値を返すisPipActiveがリアルタイムで更新される