|
|
@@ -50,8 +50,11 @@ class _VideoRecommendListPageState extends ConsumerState<VideoRecommendListPage>
|
|
|
if (controller == null) return;
|
|
|
|
|
|
try {
|
|
|
- operation();
|
|
|
+ if (controller.isVideoInitialized() == true) {
|
|
|
+ operation();
|
|
|
+ }
|
|
|
} catch (e) {
|
|
|
+ consoleLog("Controller operation error: $e");
|
|
|
// 控制器可能已被释放,重置为 null
|
|
|
globalVideoController = null;
|
|
|
}
|
|
|
@@ -76,14 +79,29 @@ class _VideoRecommendListPageState extends ConsumerState<VideoRecommendListPage>
|
|
|
BetterPlayerConfiguration(
|
|
|
autoPlay: true,
|
|
|
looping: true,
|
|
|
- aspectRatio: 16 / 9,
|
|
|
- fit: BoxFit.fitWidth,
|
|
|
+ fit: BoxFit.fitWidth, // 宽度固定,高度自适应
|
|
|
controlsConfiguration: const BetterPlayerControlsConfiguration(
|
|
|
showControls: false,
|
|
|
),
|
|
|
+ handleLifecycle: false,
|
|
|
+ autoDetectFullscreenAspectRatio: false,
|
|
|
+ errorBuilder: (context, errorMessage) {
|
|
|
+ consoleLog("GlobalVideoController error: $errorMessage");
|
|
|
+ // 返回空 widget,不显示错误
|
|
|
+ return const SizedBox.shrink();
|
|
|
+ },
|
|
|
),
|
|
|
);
|
|
|
|
|
|
+ // 先添加事件监听,然后再设置数据源
|
|
|
+ globalVideoController!.addEventsListener((event) {
|
|
|
+ consoleLog("GlobalVideoController event: ${event.betterPlayerEventType}");
|
|
|
+ if (event.betterPlayerEventType == BetterPlayerEventType.exception) {
|
|
|
+ consoleLog("Video exception occurred");
|
|
|
+ // 静默处理异常
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
Future.delayed(const Duration(milliseconds: 50), () {
|
|
|
if (!mounted || globalVideoController == null) return;
|
|
|
|
|
|
@@ -92,6 +110,9 @@ class _VideoRecommendListPageState extends ConsumerState<VideoRecommendListPage>
|
|
|
BetterPlayerDataSourceType.network,
|
|
|
url,
|
|
|
videoFormat: BetterPlayerVideoFormat.other,
|
|
|
+ notificationConfiguration: BetterPlayerNotificationConfiguration(
|
|
|
+ showNotification: false,
|
|
|
+ ),
|
|
|
);
|
|
|
|
|
|
globalVideoController!.setupDataSource(dataSource).then((_) {
|
|
|
@@ -101,10 +122,12 @@ class _VideoRecommendListPageState extends ConsumerState<VideoRecommendListPage>
|
|
|
});
|
|
|
}
|
|
|
}).catchError((error) {
|
|
|
- globalVideoController = null;
|
|
|
+ consoleLog("Video setup error: $error");
|
|
|
+ // 静默处理错误,不显示错误提示
|
|
|
});
|
|
|
} catch (e) {
|
|
|
- globalVideoController = null;
|
|
|
+ consoleLog("Video play error: $e");
|
|
|
+ // 静默处理错误,不显示错误提示
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
@@ -112,7 +135,8 @@ class _VideoRecommendListPageState extends ConsumerState<VideoRecommendListPage>
|
|
|
@override
|
|
|
void dispose() {
|
|
|
_pageController.dispose();
|
|
|
- // 不在这里 dispose 全局控制器
|
|
|
+ // 暂停播放但不dispose,因为可能被其他页面使用
|
|
|
+ globalVideoController?.pause();
|
|
|
super.dispose();
|
|
|
}
|
|
|
|
|
|
@@ -128,13 +152,18 @@ class _VideoRecommendListPageState extends ConsumerState<VideoRecommendListPage>
|
|
|
}
|
|
|
if (visible) {
|
|
|
consoleLog("页面可见");
|
|
|
- _safeControllerOperation(() {
|
|
|
- globalVideoController!.play();
|
|
|
- });
|
|
|
+ // 页面重新可见时,重新初始化视频以确保状态正确
|
|
|
+ final videos = ref.read(recommendListProvider);
|
|
|
+ if (videos.isNotEmpty && _currentPageIndex < videos.length) {
|
|
|
+ final url = videos[_currentPageIndex].url;
|
|
|
+ if (url != null && url.isNotEmpty) {
|
|
|
+ _playVideo(url);
|
|
|
+ }
|
|
|
+ }
|
|
|
} else {
|
|
|
consoleLog("页面不可见");
|
|
|
_safeControllerOperation(() {
|
|
|
- globalVideoController!.pause();
|
|
|
+ globalVideoController?.pause();
|
|
|
});
|
|
|
}
|
|
|
}
|