fix(player): 路由覆盖时暂停播放
This commit is contained in:
1 parent
911f13f464
commit
699fd427b8
5 files changed
+141
-2
No files matched your search
@@ -289,6 +289,20 @@ class PlayerLogic extends ChangeNotifier {
|
||||
}
|
||||
}
|
||||
|
||||
/// 当前播放器页面被其它 PageRoute 覆盖时暂停并保存进度。
|
||||
void onRouteCovered() {
|
||||
if (!_started) return;
|
||||
session.setRouteVisible(false);
|
||||
cancelScrub();
|
||||
unawaited(saveCurrentProgress());
|
||||
}
|
||||
|
||||
/// 覆盖播放器的 PageRoute 返回后恢复当前 Tab 的播放状态。
|
||||
void onRouteRevealed() {
|
||||
if (!_started || !isMounted()) return;
|
||||
session.setRouteVisible(active);
|
||||
}
|
||||
|
||||
void onPageUpdated(PlayerPage previous) {
|
||||
if (previous.active != active) {
|
||||
session.setRouteVisible(active && visibleIndices.isNotEmpty);
|
||||
|
||||
@@ -14,6 +14,7 @@ import '../../core/data/services/playback_session.dart';
|
||||
import '../../core/data/services/video_player_port.dart';
|
||||
import '../../core/data/services/wallet_controller.dart';
|
||||
import '../../core/formatting/error_text.dart';
|
||||
import '../../core/navigation/app_route_observer.dart';
|
||||
import '../../core/platform/haptics.dart';
|
||||
import '../../core/theme/app_colors.dart';
|
||||
import '../../core/theme/app_theme.dart';
|
||||
@@ -86,8 +87,10 @@ class PlayerPage extends StatefulWidget {
|
||||
State<PlayerPage> createState() => _PlayerPageState();
|
||||
}
|
||||
|
||||
class _PlayerPageState extends State<PlayerPage> with WidgetsBindingObserver {
|
||||
class _PlayerPageState extends State<PlayerPage>
|
||||
with WidgetsBindingObserver, RouteAware {
|
||||
late final PlayerLogic logic;
|
||||
PageRoute<dynamic>? _subscribedRoute;
|
||||
bool _chromeVisible = true;
|
||||
double _playbackSpeed = 1.0;
|
||||
bool _boosting = false;
|
||||
@@ -121,14 +124,35 @@ class _PlayerPageState extends State<PlayerPage> with WidgetsBindingObserver {
|
||||
logic.onPageUpdated(oldWidget);
|
||||
}
|
||||
|
||||
@override
|
||||
void didChangeDependencies() {
|
||||
super.didChangeDependencies();
|
||||
final route = ModalRoute.of(context);
|
||||
if (route is! PageRoute<dynamic> || route == _subscribedRoute) return;
|
||||
if (_subscribedRoute != null) appRouteObserver.unsubscribe(this);
|
||||
_subscribedRoute = route;
|
||||
appRouteObserver.subscribe(this, route);
|
||||
}
|
||||
|
||||
@override
|
||||
void didChangeAppLifecycleState(AppLifecycleState state) {
|
||||
logic.onLifecycle(state);
|
||||
}
|
||||
|
||||
@override
|
||||
void didPushNext() {
|
||||
logic.onRouteCovered();
|
||||
}
|
||||
|
||||
@override
|
||||
void didPopNext() {
|
||||
logic.onRouteRevealed();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
WidgetsBinding.instance.removeObserver(this);
|
||||
appRouteObserver.unsubscribe(this);
|
||||
logic.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user