feat(player): 接入双端画中画播放
This commit is contained in:
1 parent
5346be40a9
commit
43e60e29ec
10 files changed
+1015
-29
No files matched your search
@@ -3,7 +3,8 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
/// Native Android activity PiP; the Flutter playback session keeps the player.
|
||||
/// 原生活动级画中画桥:Android 是 Activity PiP(画面即 Flutter 渲染),
|
||||
/// iOS 是影子播放器接力(画面/声音来自原生 AVPlayer,退出时进度回传)。
|
||||
class AndroidPictureInPicture {
|
||||
AndroidPictureInPicture({MethodChannel? channel})
|
||||
: _channel = channel ?? const MethodChannel('com.stormg.lumadrama/pip');
|
||||
@@ -11,22 +12,147 @@ class AndroidPictureInPicture {
|
||||
final MethodChannel _channel;
|
||||
|
||||
bool get isPlatformSupported =>
|
||||
!kIsWeb && defaultTargetPlatform == TargetPlatform.android;
|
||||
!kIsWeb &&
|
||||
(defaultTargetPlatform == TargetPlatform.android ||
|
||||
defaultTargetPlatform == TargetPlatform.iOS);
|
||||
|
||||
void setStateListener(ValueChanged<bool>? listener) {
|
||||
_channel.setMethodCallHandler(
|
||||
listener == null
|
||||
? null
|
||||
: (call) async {
|
||||
if (call.method == 'onPipChanged') {
|
||||
listener(call.arguments == true);
|
||||
}
|
||||
},
|
||||
);
|
||||
void Function(bool active, int finalPositionMs)? _pipChangedListener;
|
||||
VoidCallback? _playbackEndedListener;
|
||||
|
||||
/// PiP 状态回调:iOS 停止时 [finalPositionMs] 为影子最终进度(毫秒)。
|
||||
void setStateListener(void Function(bool active, int finalPositionMs)? listener) {
|
||||
_pipChangedListener = listener;
|
||||
_refreshHandler();
|
||||
}
|
||||
|
||||
Future<bool> enter() async {
|
||||
/// iOS 画中画内影子播完当前集(Android 不会触发)。
|
||||
void setPlaybackEndedListener(VoidCallback? listener) {
|
||||
_playbackEndedListener = listener;
|
||||
_refreshHandler();
|
||||
}
|
||||
|
||||
void _refreshHandler() {
|
||||
if (_pipChangedListener == null && _playbackEndedListener == null) {
|
||||
_channel.setMethodCallHandler(null);
|
||||
return;
|
||||
}
|
||||
_channel.setMethodCallHandler((call) async {
|
||||
switch (call.method) {
|
||||
case 'pipDebug':
|
||||
debugPrint('pip[native]: ${call.arguments}');
|
||||
case 'onPipChanged':
|
||||
final listener = _pipChangedListener;
|
||||
if (listener == null) return;
|
||||
final args = call.arguments;
|
||||
if (args is Map) {
|
||||
listener(
|
||||
args['active'] == true,
|
||||
(args['positionMs'] as num?)?.toInt() ?? 0,
|
||||
);
|
||||
} else {
|
||||
// Android 只回传 bool。
|
||||
listener(args == true, 0);
|
||||
}
|
||||
case 'onPipPlaybackEnded':
|
||||
_playbackEndedListener?.call();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// 进入画中画。Android 忽略片源参数;iOS 用 [url] 与 [positionMs]
|
||||
/// 初始化影子播放器(对齐到当前进度、静音待命)。
|
||||
Future<bool> enter({required String url, required int positionMs}) async {
|
||||
if (!isPlatformSupported) return false;
|
||||
return await _channel.invokeMethod<bool>('enter') ?? false;
|
||||
return await _channel.invokeMethod<bool>('enter', {
|
||||
'url': url,
|
||||
'positionMs': positionMs,
|
||||
}) ??
|
||||
false;
|
||||
}
|
||||
|
||||
/// 创建影子播放器但不起播(仅 iOS):
|
||||
/// 自动小窗待命形态——静音暂停,退后台由 [armAutoEnter] 拉起。
|
||||
Future<void> prepareShadow({
|
||||
required String url,
|
||||
required int positionMs,
|
||||
bool autoEnter = false,
|
||||
}) async {
|
||||
if (defaultTargetPlatform != TargetPlatform.iOS || kIsWeb) return;
|
||||
try {
|
||||
await _channel.invokeMethod<void>('prepareShadow', {
|
||||
'url': url,
|
||||
'positionMs': positionMs,
|
||||
'autoEnter': autoEnter,
|
||||
});
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
/// 退后台瞬间把影子对齐到当前进度并静音起播,触发系统自动画中画(仅 iOS)。
|
||||
Future<void> armAutoEnter(int positionMs) async {
|
||||
if (defaultTargetPlatform != TargetPlatform.iOS || kIsWeb) return;
|
||||
try {
|
||||
await _channel.invokeMethod<void>('armAutoEnter', {
|
||||
'positionMs': positionMs,
|
||||
});
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
/// 回前台后影子回待命(暂停、静音)(仅 iOS)。
|
||||
Future<void> disarmAutoEnter() async {
|
||||
if (defaultTargetPlatform != TargetPlatform.iOS || kIsWeb) return;
|
||||
try {
|
||||
await _channel.invokeMethod<void>('disarmAutoEnter');
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
/// 显示原生还原过渡层(仅 iOS;还原动画开始时原生侧也会自动盖上)。
|
||||
Future<void> showRestoreOverlay() async {
|
||||
if (defaultTargetPlatform != TargetPlatform.iOS || kIsWeb) return;
|
||||
try {
|
||||
await _channel.invokeMethod<void>('showRestoreOverlay');
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
/// 淡出原生还原过渡层(仅 iOS;视频首帧确认后调用)。
|
||||
Future<void> hideRestoreOverlay() async {
|
||||
if (defaultTargetPlatform != TargetPlatform.iOS || kIsWeb) return;
|
||||
try {
|
||||
await _channel.invokeMethod<void>('hideRestoreOverlay');
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
/// 解除影子静音并起播(仅 iOS 有意义,进入 PiP 后声音交给影子)。
|
||||
Future<void> unmute() async {
|
||||
if (defaultTargetPlatform != TargetPlatform.iOS || kIsWeb) return;
|
||||
try {
|
||||
await _channel.invokeMethod<void>('unmute');
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
/// iOS 画中画内换集:影子只换播放项,窗口不中断。
|
||||
Future<void> replaceItem({required String url, required int positionMs}) async {
|
||||
if (defaultTargetPlatform != TargetPlatform.iOS || kIsWeb) return;
|
||||
try {
|
||||
await _channel.invokeMethod<void>('replaceItem', {
|
||||
'url': url,
|
||||
'positionMs': positionMs,
|
||||
});
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
/// 请求退出画中画(iOS;触发 onPipChanged(false) 后由 Dart 收尾)。
|
||||
Future<void> stop() async {
|
||||
if (defaultTargetPlatform != TargetPlatform.iOS || kIsWeb) return;
|
||||
try {
|
||||
await _channel.invokeMethod<void>('stop');
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
/// 销毁影子播放器(仅 iOS)。
|
||||
Future<void> teardown() async {
|
||||
if (defaultTargetPlatform != TargetPlatform.iOS || kIsWeb) return;
|
||||
try {
|
||||
await _channel.invokeMethod<void>('teardown');
|
||||
} catch (_) {}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user