基于自定义规则的番剧采集APP,支持流媒体在线观看,支持弹幕,支持实时超分辨率。
feat: add syncplay chat danmaku (#1654)
UzawaReisaQwQ583ba87c0bd1
diff --git a/lib/pages/player/player_controller.dart b/lib/pages/player/player_controller.dartindex 455fb0c3..ef22e69e 100644--- a/lib/pages/player/player_controller.dart+++ b/lib/pages/player/player_controller.dart@@ -25,6 +25,25 @@ import 'package:url_launcher/url_launcher_string.dart';part 'player_controller.g.dart';+enum DanmakuDestination {+ chatRoom,+ remoteDanmaku,+}++class SyncPlayChatMessage {+ final String username;+ final String message;+ final bool fromRemote;+ final DateTime time;++ SyncPlayChatMessage({+ required this.username,+ required this.message,+ this.fromRemote = true,+ DateTime? time,+ }) : time = time ?? DateTime.now();+}+class PlayerController = _PlayerController with _$PlayerController;abstract class _PlayerController with Store {@@ -40,6 +59,11 @@ abstract class _PlayerController with Store {bool danmakuOn = false;@observablebool danmakuLoading = false;+ DanmakuDestination danmakuDestination = DanmakuDestination.remoteDanmaku;+ final StreamController<SyncPlayChatMessage> syncPlayChatStreamController =+ StreamController<SyncPlayChatMessage>.broadcast();+ Stream<SyncPlayChatMessage> get syncPlayChatStream =>+ syncPlayChatStreamController.stream;// 一起看控制器SyncplayClient? syncplayController;@@ -648,7 +672,7 @@ abstract class _PlayerController with Store {String username,Future<void> Function(int episode, {int currentRoad, int offset})changeEpisode,- {bool enableTLS = false}) async {+ {bool enableTLS = true}) async {await syncplayController?.disconnect();final String syncPlayEndPoint = setting.get(SettingBoxKey.syncPlayEndPoint,defaultValue: defaultSyncPlayEndPoint);@@ -744,13 +768,22 @@ abstract class _PlayerController with Store {);syncplayController!.onChatMessage.listen((message) {- if (message['username'] != username) {- KazumiDialog.showToast(- message:- 'SyncPlay: ${message['username']} 说: ${message['message']}',- duration: const Duration(seconds: 5));+ final String sender = (message['username'] ?? '').toString();+ final String text = (message['message'] ?? '').toString();+ final bool fromRemote = message['username'] != username;++ // 将消息转发到流+ if (!syncPlayChatStreamController.isClosed) {+ syncPlayChatStreamController.add(SyncPlayChatMessage(+ username: sender,+ message: text,+ fromRemote: fromRemote,+ ));}},+ onError: (error) {+ print('SyncPlay: error: ${error.message}');+ },);syncplayController!.onPositionChangedMessage.listen((message) {diff --git a/lib/pages/player/player_item.dart b/lib/pages/player/player_item.dartindex 80a8877f..9dfcffb5 100644--- a/lib/pages/player/player_item.dart+++ b/lib/pages/player/player_item.dart@@ -43,6 +43,7 @@ class PlayerItem extends StatefulWidget {required this.onBackPressed,required this.keyboardFocus,required this.sendDanmaku,+ required this.showDanmakuDestinationPickerAndSend,this.disableAnimations = false,});@@ -54,6 +55,7 @@ class PlayerItem extends StatefulWidget {final void Function(String) sendDanmaku;final FocusNode keyboardFocus;final bool disableAnimations;+ final void Function(String) showDanmakuDestinationPickerAndSend;@overrideState<PlayerItem> createState() => _PlayerItemState();@@ -1511,6 +1513,7 @@ class _PlayerItemState extends State<PlayerItem>showSyncPlayRoomCreateDialog,showSyncPlayEndPointSwitchDialog:showSyncPlayEndPointSwitchDialog,+ showDanmakuDestinationPickerAndSend: widget.showDanmakuDestinationPickerAndSend,disableAnimations: widget.disableAnimations,handleScreenShot: handleScreenshot,skipOP: skipOP,diff --git a/lib/pages/player/player_item_panel.dart b/lib/pages/player/player_item_panel.dartindex 038a646b..3a0589e9 100644--- a/lib/pages/player/player_item_panel.dart+++ b/lib/pages/player/player_item_panel.dart@@ -41,6 +41,7 @@ class PlayerItemPanel extends StatefulWidget {required this.showVideoInfo,required this.showSyncPlayRoomCreateDialog,required this.showSyncPlayEndPointSwitchDialog,+ required this.showDanmakuDestinationPickerAndSend,this.disableAnimations = false,});@@ -65,6 +66,7 @@ class PlayerItemPanel extends StatefulWidget {final void Function() showVideoInfo;final void Function() showSyncPlayRoomCreateDialog;final void Function() showSyncPlayEndPointSwitchDialog;+ final void Function(String) showDanmakuDestinationPickerAndSend;final bool disableAnimations;@override@@ -119,24 +121,30 @@ class _PlayerItemPanelState extends State<PlayerItemPanel> {borderRadius:BorderRadius.all(Radius.circular(Utils.isDesktop() ? 8 : 20)),),- suffixIcon: TextButton(- onPressed: () {- textFieldFocus.unfocus();- widget.sendDanmaku(textController.text);- textController.clear();- },- style: TextButton.styleFrom(- foregroundColor: playerController.danmakuOn- ? Theme.of(context).colorScheme.onPrimaryContainer- : Colors.white60,- backgroundColor: playerController.danmakuOn- ? Theme.of(context).colorScheme.primaryContainer- : Theme.of(context).disabledColor,- shape: RoundedRectangleBorder(- borderRadius: BorderRadius.circular(Utils.isDesktop() ? 8 : 20),+ suffixIconConstraints: const BoxConstraints(minWidth: 0),+ suffixIcon: Row(+ mainAxisSize: MainAxisSize.min,+ children: [+ TextButton(+ onPressed: () {+ textFieldFocus.unfocus();+ widget.showDanmakuDestinationPickerAndSend(textController.text);+ textController.clear();+ },+ style: TextButton.styleFrom(+ foregroundColor: playerController.danmakuOn+ ? Theme.of(context).colorScheme.onPrimaryContainer+ : Colors.white60,+ backgroundColor: playerController.danmakuOn+ ? Theme.of(context).colorScheme.primaryContainer+ : Theme.of(context).disabledColor,+ shape: RoundedRectangleBorder(+ borderRadius: BorderRadius.circular(Utils.isDesktop() ? 8 : 20),+ ),+ ),+ child: const Text('发送'),),- ),- child: const Text('发送'),+ ],),),onTapAlwaysCalled: true,@@ -146,7 +154,7 @@ class _PlayerItemPanelState extends State<PlayerItemPanel> {},onSubmitted: (msg) {textFieldFocus.unfocus();- widget.sendDanmaku(msg);+ widget.showDanmakuDestinationPickerAndSend(msg);widget.cancelHideTimer();widget.startHideTimer();playerController.canHidePlayerPanel = true;diff --git a/lib/pages/video/video_page.dart b/lib/pages/video/video_page.dartindex 1a70505c..cd898201 100644--- a/lib/pages/video/video_page.dart+++ b/lib/pages/video/video_page.dart@@ -69,6 +69,9 @@ class _VideoPageState extends State<VideoPage>// disable animation.late final bool disableAnimations;+ // SyncPlayChatMessage+ late final StreamSubscription<SyncPlayChatMessage> _syncChatSubscription;+@overridevoid initState() {super.initState();@@ -151,6 +154,23 @@ class _VideoPageState extends State<VideoPage>webviewLogLines.add(event);});});+ _syncChatSubscription = playerController.syncPlayChatStream.listen((event) {+ final localUsername = playerController.syncplayController?.username ?? '';+ final String displayText = '${event.username}:${event.message}';++ // 只有在弹幕开启时渲染弹幕并确保是别人发送的弹幕+ if (playerController.danmakuOn && event.username != localUsername && event.fromRemote) {+ playerController.danmakuController.addDanmaku(+ DanmakuContentItem(+ displayText,+ color: Colors.orange,+ isColorful: true,+ type: DanmakuItemType.bottom,+ extra: DateTime.now().millisecondsSinceEpoch,+ ),+ );+ }+ });}@override@@ -176,6 +196,9 @@ class _VideoPageState extends State<VideoPage>try {_logSubscription.cancel();} catch (_) {}+ try {+ _syncChatSubscription.cancel();+ } catch (_) {}try {playerController.dispose();} catch (e) {@@ -310,10 +333,37 @@ class _VideoPageState extends State<VideoPage>KazumiDialog.showToast(message: '弹幕内容过长');return;}- // Todo 接口方限制- playerController.danmakuController+ final destination = playerController.danmakuDestination;++ if (destination == DanmakuDestination.chatRoom) {+ if (playerController.syncplayRoom.isEmpty) {+ KazumiDialog.showToast(message: '你还没有加入一起看,无法发送聊天室弹幕');+ return;+ }++ final sender = playerController.syncplayController?.username ?? '我';+ final String displayText = '$sender:$msg';++ // 在播放器渲染自己发送的弹幕+ playerController.danmakuController.addDanmaku(+ DanmakuContentItem(+ displayText,+ color: Colors.orange,+ isColorful: true,+ type: DanmakuItemType.bottom,+ extra: DateTime.now().millisecondsSinceEpoch,+ ),+ );++ // 发送弹幕到聊天室+ playerController.sendSyncPlayChatMessage(msg);+ } else {+ // Todo 接口方限制++ playerController.danmakuController.addDanmaku(DanmakuContentItem(msg, selfSend: true));+ }}void showMobileDanmakuInput() {@@ -323,59 +373,103 @@ class _VideoPageState extends State<VideoPage>isScrollControlled: true,context: context,builder: (context) {- return Padding(- padding: EdgeInsets.only(- bottom: MediaQuery.of(context).viewInsets.bottom,- left: 8,- ),- child: Row(- mainAxisAlignment: MainAxisAlignment.center,- children: [- Expanded(- child: Container(- constraints: const BoxConstraints(maxHeight: 34),- child: TextField(- style: const TextStyle(fontSize: 15),- controller: textController,- autofocus: true,- textAlignVertical: TextAlignVertical.center,- decoration: const InputDecoration(- filled: true,- floatingLabelBehavior: FloatingLabelBehavior.never,- hintText: '发个友善的弹幕见证当下',- hintStyle: TextStyle(fontSize: 14),- alignLabelWithHint: true,- contentPadding:- EdgeInsets.symmetric(vertical: 8, horizontal: 12),- border: OutlineInputBorder(- borderSide: BorderSide.none,- borderRadius: BorderRadius.all(Radius.circular(20)),+ return StatefulBuilder(+ builder: (context, setModalState) {+ return Padding(+ padding: EdgeInsets.only(+ bottom: MediaQuery.of(context).viewInsets.bottom,+ left: 8,+ ),+ child: Row(+ mainAxisAlignment: MainAxisAlignment.center,+ children: [+ Expanded(+ child: Container(+ constraints: const BoxConstraints(maxHeight: 34),+ child: TextField(+ style: const TextStyle(fontSize: 15),+ controller: textController,+ autofocus: true,+ textAlignVertical: TextAlignVertical.center,+ decoration: const InputDecoration(+ filled: true,+ floatingLabelBehavior: FloatingLabelBehavior.never,+ hintText: '发个友善的弹幕见证当下',+ hintStyle: TextStyle(fontSize: 14),+ alignLabelWithHint: true,+ contentPadding:+ EdgeInsets.symmetric(vertical: 8, horizontal: 12),+ border: OutlineInputBorder(+ borderSide: BorderSide.none,+ borderRadius: BorderRadius.all(Radius.circular(20)),+ ),+ ),+ onSubmitted: (msg) {+ showDanmakuDestinationPickerAndSend(msg);+ textController.clear();+ Navigator.pop(context);+ },),),- onSubmitted: (msg) {- sendDanmaku(msg);- textController.clear();+ ),+ IconButton(+ onPressed: () {+ final msg = textController.text;Navigator.pop(context);+ showDanmakuDestinationPickerAndSend(msg);+ textController.clear();},- ),- ),+ icon: Icon(+ Icons.send_rounded,+ color: Theme.of(context).colorScheme.primary,+ ),+ )+ ],),- IconButton(- onPressed: () {- sendDanmaku(textController.text);- textController.clear();- Navigator.pop(context);- },- icon: Icon(- Icons.send_rounded,- color: Theme.of(context).colorScheme.primary,- ),- )+ );+ },+ );+ },+ );+ }++ void showDanmakuDestinationPickerAndSend(String msg) async {+ if (msg.trim().isEmpty) {+ KazumiDialog.showToast(message: '弹幕内容为空');+ return;+ }++ final DanmakuDestination? result = await showModalBottomSheet<DanmakuDestination>(+ context: context,+ shape: const BeveledRectangleBorder(),+ builder: (context) {+ return SafeArea(+ left: false,+ right: false,+ child: Column(+ mainAxisSize: MainAxisSize.min,+ children: [+ ListTile(+ title: const Text('发送到聊天室'),+ onTap: () => Navigator.of(context).pop(DanmakuDestination.chatRoom),+ ),+ ListTile(+ title: const Text('发送到远程弹幕库'),+ onTap: () => Navigator.of(context).pop(DanmakuDestination.remoteDanmaku),+ ),+ const SizedBox(height: 8),],),);},);++ if (result != null) {+ setState(() {+ });+ playerController.danmakuDestination = result;+ sendDanmaku(msg);+ }}@override@@ -664,6 +758,7 @@ class _VideoPageState extends State<VideoPage>keyboardFocus: keyboardFocus,sendDanmaku: sendDanmaku,disableAnimations: disableAnimations,+ showDanmakuDestinationPickerAndSend: showDanmakuDestinationPickerAndSend,),),