Skip to content

基于自定义规则的番剧采集APP,支持流媒体在线观看,支持弹幕,支持实时超分辨率。

refactor: decoup castvideo and external player && move syncplay to submenu

Predidit9f4df49b8b80
diff --git a/lib/pages/player/player_controller.dart b/lib/pages/player/player_controller.dart
index b9fff24d..f210184f 100644
--- a/lib/pages/player/player_controller.dart
+++ b/lib/pages/player/player_controller.dart
@@ -21,6 +21,8 @@ import 'package:flutter/services.dart';
import 'package:kazumi/utils/constants.dart';
import 'package:kazumi/shaders/shaders_controller.dart';
import 'package:kazumi/utils/syncplay.dart';
+import 'package:kazumi/utils/external_player.dart';
+import 'package:url_launcher/url_launcher_string.dart';
part 'player_controller.g.dart';
@@ -444,6 +446,55 @@ abstract class _PlayerController with Store {
}
}
+ void lanunchExternalPlayer() async {
+ String referer = videoPageController.currentPlugin.referer;
+ if ((Platform.isAndroid || Platform.isWindows) && referer.isEmpty) {
+ if (await ExternalPlayer.launchURLWithMIME(videoUrl, 'video/mp4')) {
+ KazumiDialog.dismiss();
+ KazumiDialog.showToast(
+ message: '尝试唤起外部播放器',
+ );
+ } else {
+ KazumiDialog.showToast(
+ message: '唤起外部播放器失败',
+ );
+ }
+ } else if (Platform.isMacOS || Platform.isIOS) {
+ if (await ExternalPlayer.launchURLWithReferer(videoUrl, referer)) {
+ KazumiDialog.dismiss();
+ KazumiDialog.showToast(
+ message: '尝试唤起外部播放器',
+ );
+ } else {
+ KazumiDialog.showToast(
+ message: '唤起外部播放器失败',
+ );
+ }
+ } else if (Platform.isLinux && referer.isEmpty) {
+ KazumiDialog.dismiss();
+ if (await canLaunchUrlString(videoUrl)) {
+ launchUrlString(videoUrl);
+ KazumiDialog.showToast(
+ message: '尝试唤起外部播放器',
+ );
+ } else {
+ KazumiDialog.showToast(
+ message: '无法使用外部播放器',
+ );
+ }
+ } else {
+ if (referer.isEmpty) {
+ KazumiDialog.showToast(
+ message: '暂不支持该设备',
+ );
+ } else {
+ KazumiDialog.showToast(
+ message: '暂不支持该规则',
+ );
+ }
+ }
+ }
+
Future<void> createSyncPlayRoom(
String room, String username, Function changeEpisode) async {
await syncplayController?.disconnect();
diff --git a/lib/pages/player/player_item.dart b/lib/pages/player/player_item.dart
index b95603d9..0a3913ec 100644
--- a/lib/pages/player/player_item.dart
+++ b/lib/pages/player/player_item.dart
@@ -537,10 +537,13 @@ class _PlayerItemState extends State<PlayerItem>
width: 400,
child: TextDisplayWidget(logLines: playerController.playerLog)),
actions: [
- TextButton(onPressed: () {
- Clipboard.setData(ClipboardData(text: playerController.playerLog.toString()));
- KazumiDialog.showToast(message: '已复制到剪贴板');
- }, child: const Text('复制到剪贴板')),
+ TextButton(
+ onPressed: () {
+ Clipboard.setData(
+ ClipboardData(text: playerController.playerLog.toString()));
+ KazumiDialog.showToast(message: '已复制到剪贴板');
+ },
+ child: const Text('复制到剪贴板')),
TextButton(
onPressed: KazumiDialog.dismiss,
child: const Text('取消'),
@@ -550,6 +553,77 @@ class _PlayerItemState extends State<PlayerItem>
});
}
+ void showSyncPlayRoomCreateDialog() {
+ final formKey = GlobalKey<FormState>();
+ final TextEditingController roomController = TextEditingController();
+ final TextEditingController usernameController = TextEditingController();
+ KazumiDialog.show(builder: (BuildContext context) {
+ return AlertDialog(
+ title: const Text('创建房间'),
+ content: Form(
+ key: formKey,
+ child: Column(
+ mainAxisSize: MainAxisSize.min,
+ children: [
+ TextFormField(
+ controller: roomController,
+ keyboardType: TextInputType.number,
+ decoration: const InputDecoration(
+ labelText: '房间号',
+ ),
+ validator: (value) {
+ if (value == null || value.isEmpty) {
+ return '请输入房间号';
+ }
+ final regex = RegExp(r'^[0-9]{6,10}$');
+ if (!regex.hasMatch(value)) {
+ return '房间号需要6到10位数字';
+ }
+ return null;
+ },
+ ),
+ const SizedBox(height: 16),
+ TextFormField(
+ controller: usernameController,
+ decoration: const InputDecoration(
+ labelText: '用户名',
+ ),
+ validator: (value) {
+ if (value == null || value.isEmpty) {
+ return '请输入用户名';
+ }
+ final regex = RegExp(r'^[a-zA-Z]{4,12}$');
+ if (!regex.hasMatch(value)) {
+ return '用户名必须为4到12位英文字符';
+ }
+ return null;
+ },
+ ),
+ ],
+ ),
+ ),
+ actions: [
+ TextButton(
+ onPressed: () {
+ KazumiDialog.dismiss();
+ },
+ child: const Text('取消'),
+ ),
+ TextButton(
+ onPressed: () {
+ if (formKey.currentState!.validate()) {
+ KazumiDialog.dismiss();
+ playerController.createSyncPlayRoom(roomController.text,
+ usernameController.text, widget.changeEpisode);
+ }
+ },
+ child: const Text('确定'),
+ ),
+ ],
+ );
+ });
+ }
+
/// Used to decide which panel is used.
/// It's too complicated to write these in conditional sentence.
/// * true: use [PlayerItemPanel]
@@ -907,6 +981,8 @@ class _PlayerItemState extends State<PlayerItem>
cancelHideTimer: cancelHideTimer,
handleDanmaku: handleDanmaku,
showVideoInfo: showVideoInfo,
+ showSyncPlayRoomCreateDialog:
+ showSyncPlayRoomCreateDialog,
)
: SmallestPlayerItemPanel(
onBackPressed: widget.onBackPressed,
@@ -923,6 +999,8 @@ class _PlayerItemState extends State<PlayerItem>
cancelHideTimer: cancelHideTimer,
handleDanmaku: handleDanmaku,
showVideoInfo: showVideoInfo,
+ showSyncPlayRoomCreateDialog:
+ showSyncPlayRoomCreateDialog,
),
// 播放器手势控制
Positioned.fill(
diff --git a/lib/pages/player/player_item_panel.dart b/lib/pages/player/player_item_panel.dart
index e7edcebf..6da00c4e 100644
--- a/lib/pages/player/player_item_panel.dart
+++ b/lib/pages/player/player_item_panel.dart
@@ -37,6 +37,7 @@ class PlayerItemPanel extends StatefulWidget {
required this.cancelHideTimer,
required this.handleDanmaku,
required this.showVideoInfo,
+ required this.showSyncPlayRoomCreateDialog,
});
final void Function(BuildContext) onBackPressed;
@@ -54,6 +55,7 @@ class PlayerItemPanel extends StatefulWidget {
final void Function() handleDanmaku;
final void Function(String) sendDanmaku;
final void Function() showVideoInfo;
+ final void Function() showSyncPlayRoomCreateDialog;
@override
State<PlayerItemPanel> createState() => _PlayerItemPanelState();
@@ -214,77 +216,6 @@ class _PlayerItemPanelState extends State<PlayerItemPanel> {
});
}
- void showSyncPlayRoomCreateDialog() {
- final formKey = GlobalKey<FormState>();
- final TextEditingController roomController = TextEditingController();
- final TextEditingController usernameController = TextEditingController();
- KazumiDialog.show(builder: (BuildContext context) {
- return AlertDialog(
- title: const Text('创建房间'),
- content: Form(
- key: formKey,
- child: Column(
- mainAxisSize: MainAxisSize.min,
- children: [
- TextFormField(
- controller: roomController,
- keyboardType: TextInputType.number,
- decoration: const InputDecoration(
- labelText: '房间号',
- ),
- validator: (value) {
- if (value == null || value.isEmpty) {
- return '请输入房间号';
- }
- final regex = RegExp(r'^[0-9]{6,10}$');
- if (!regex.hasMatch(value)) {
- return '房间号需要6到10位数字';
- }
- return null;
- },
- ),
- const SizedBox(height: 16),
- TextFormField(
- controller: usernameController,
- decoration: const InputDecoration(
- labelText: '用户名',
- ),
- validator: (value) {
- if (value == null || value.isEmpty) {
- return '请输入用户名';
- }
- final regex = RegExp(r'^[a-zA-Z]{4,12}$');
- if (!regex.hasMatch(value)) {
- return '用户名必须为4到12位英文字符';
- }
- return null;
- },
- ),
- ],
- ),
- ),
- actions: [
- TextButton(
- onPressed: () {
- KazumiDialog.dismiss();
- },
- child: const Text('取消'),
- ),
- TextButton(
- onPressed: () {
- if (formKey.currentState!.validate()) {
- KazumiDialog.dismiss();
- playerController.createSyncPlayRoom(
- roomController.text, usernameController.text, widget.changeEpisode);
- }
- },
- child: const Text('确定'),
- ),
- ],
- );
- });
- }
-
void showForwardChange() {
KazumiDialog.show(builder: (context) {
String input = "";
@@ -738,7 +669,7 @@ class _PlayerItemPanelState extends State<PlayerItemPanel> {
playerController.pause();
RemotePlay()
.castVideo(
- context,
+ playerController.videoUrl,
videoPageController
.currentPlugin.referer)
.whenComplete(() {
@@ -749,9 +680,62 @@ class _PlayerItemPanelState extends State<PlayerItemPanel> {
},
child: const Padding(
padding: EdgeInsets.fromLTRB(0, 10, 10, 10),
- child: Text("远程播放"),
+ child: Text("远程投屏"),
),
),
+ MenuItemButton(
+ onPressed: () {
+ playerController.lanunchExternalPlayer();
+ },
+ child: const Padding(
+ padding: EdgeInsets.fromLTRB(0, 10, 10, 10),
+ child: Text("外部播放"),
+ ),
+ ),
+ SubmenuButton(
+ menuChildren: [
+ MenuItemButton(
+ child: Padding(
+ padding:
+ EdgeInsets.fromLTRB(0, 10, 10, 10),
+ child: Text(
+ "当前房间: ${playerController.syncplayRoom == '' ? '未加入' : playerController.syncplayRoom}"),
+ ),
+ ),
+ MenuItemButton(
+ child: Padding(
+ padding:
+ EdgeInsets.fromLTRB(0, 10, 10, 10),
+ child: Text(
+ "网络延时: ${playerController.syncplayClientRtt}ms"),
+ ),
+ ),
+ MenuItemButton(
+ onPressed: () {
+ widget.showSyncPlayRoomCreateDialog();
+ },
+ child: const Padding(
+ padding:
+ EdgeInsets.fromLTRB(0, 10, 10, 10),
+ child: Text("加入房间"),
+ ),
+ ),
+ MenuItemButton(
+ onPressed: () async {
+ await playerController
+ .exitSyncPlayRoom();
+ },
+ child: const Padding(
+ padding:
+ EdgeInsets.fromLTRB(0, 10, 10, 10),
+ child: Text("断开连接"),
+ ),
+ ),
+ ],
+ child: const Padding(
+ padding:
+ EdgeInsets.fromLTRB(0, 10, 10, 10),
+ child: Text("一起看"))),
],
),
],
@@ -1027,74 +1011,6 @@ class _PlayerItemPanelState extends State<PlayerItemPanel> {
],
if (!playerController.danmakuOn) const Spacer(),
],
-
- /// 一起看 (未完成)
- MenuAnchor(
- consumeOutsideTap: true,
- onOpen: () {
- widget.cancelHideTimer();
- playerController.canHidePlayerPanel = false;
- },
- onClose: () {
- widget.cancelHideTimer();
- widget.startHideTimer();
- playerController.canHidePlayerPanel = true;
- },
- builder: (BuildContext context,
- MenuController controller, Widget? child) {
- return TextButton(
- onPressed: () {
- if (controller.isOpen) {
- controller.close();
- } else {
- controller.open();
- }
- },
- child: const Text(
- '一起看',
- style: TextStyle(color: Colors.white),
- ),
- );
- },
- menuChildren: [
- MenuItemButton(
- child: Padding(
- padding:
- EdgeInsets.fromLTRB(0, 10, 10, 10),
- child: Text(
- "当前房间: ${playerController.syncplayRoom == '' ? '未加入' : playerController.syncplayRoom}"),
- ),
- ),
- MenuItemButton(
- child: Padding(
- padding:
- EdgeInsets.fromLTRB(0, 10, 10, 10),
- child: Text(
- "网络延时: ${playerController.syncplayClientRtt}ms"),
- ),
- ),
- MenuItemButton(
- onPressed: () {
- showSyncPlayRoomCreateDialog();
- },
- child: const Padding(
- padding:
- EdgeInsets.fromLTRB(0, 10, 10, 10),
- child: Text("加入房间"),
- ),
- ),
- MenuItemButton(
- onPressed: () async {
- await playerController.exitSyncPlayRoom();
- },
- child: const Padding(
- padding:
- EdgeInsets.fromLTRB(0, 10, 10, 10),
- child: Text("断开连接"),
- ),
- ),
- ],
- ),
// 超分辨率
MenuAnchor(
consumeOutsideTap: true,
diff --git a/lib/pages/player/smallest_player_item_panel.dart b/lib/pages/player/smallest_player_item_panel.dart
index ba53fc33..137a59ab 100644
--- a/lib/pages/player/smallest_player_item_panel.dart
+++ b/lib/pages/player/smallest_player_item_panel.dart
@@ -34,6 +34,7 @@ class SmallestPlayerItemPanel extends StatefulWidget {
required this.cancelHideTimer,
required this.handleDanmaku,
required this.showVideoInfo,
+ required this.showSyncPlayRoomCreateDialog,
});
final void Function(BuildContext) onBackPressed;
@@ -49,6 +50,7 @@ class SmallestPlayerItemPanel extends StatefulWidget {
final void Function() startHideTimer;
final void Function() cancelHideTimer;
final void Function() showVideoInfo;
+ final void Function() showSyncPlayRoomCreateDialog;
@override
State<SmallestPlayerItemPanel> createState() =>
@@ -527,6 +529,44 @@ class _SmallestPlayerItemPanelState extends State<SmallestPlayerItemPanel> {
child: Text("超分辨率"),
),
),
+ SubmenuButton(
+ menuChildren: [
+ MenuItemButton(
+ child: Padding(
+ padding: EdgeInsets.fromLTRB(0, 10, 10, 10),
+ child: Text(
+ "当前房间: ${playerController.syncplayRoom == '' ? '未加入' : playerController.syncplayRoom}"),
+ ),
+ ),
+ MenuItemButton(
+ child: Padding(
+ padding: EdgeInsets.fromLTRB(0, 10, 10, 10),
+ child: Text(
+ "网络延时: ${playerController.syncplayClientRtt}ms"),
+ ),
+ ),
+ MenuItemButton(
+ onPressed: () {
+ widget.showSyncPlayRoomCreateDialog();
+ },
+ child: const Padding(
+ padding: EdgeInsets.fromLTRB(0, 10, 10, 10),
+ child: Text("加入房间"),
+ ),
+ ),
+ MenuItemButton(
+ onPressed: () async {
+ await playerController.exitSyncPlayRoom();
+ },
+ child: const Padding(
+ padding: EdgeInsets.fromLTRB(0, 10, 10, 10),
+ child: Text("断开连接"),
+ ),
+ ),
+ ],
+ child: const Padding(
+ padding: EdgeInsets.fromLTRB(0, 10, 10, 10),
+ child: Text("一起看"))),
MenuItemButton(
onPressed: () {
widget.showDanmakuSwitch();
@@ -576,7 +616,7 @@ class _SmallestPlayerItemPanelState extends State<SmallestPlayerItemPanel> {
bool needRestart = playerController.playing;
playerController.pause();
RemotePlay()
- .castVideo(context,
+ .castVideo(playerController.videoUrl,
videoPageController.currentPlugin.referer)
.whenComplete(() {
if (needRestart) {
@@ -586,7 +626,16 @@ class _SmallestPlayerItemPanelState extends State<SmallestPlayerItemPanel> {
},
child: const Padding(
padding: EdgeInsets.fromLTRB(0, 10, 10, 10),
- child: Text("远程播放"),
+ child: Text("远程投屏"),
+ ),
+ ),
+ MenuItemButton(
+ onPressed: () {
+ playerController.lanunchExternalPlayer();
+ },
+ child: const Padding(
+ padding: EdgeInsets.fromLTRB(0, 10, 10, 10),
+ child: Text("外部播放"),
),
),
],
diff --git a/lib/utils/external_player.dart b/lib/utils/external_player.dart
new file mode 100644
index 00000000..055afdc6
--- /dev/null
+++ b/lib/utils/external_player.dart
@@ -0,0 +1,34 @@
+import 'package:flutter/services.dart';
+import 'package:logger/logger.dart';
+import 'package:kazumi/utils/logger.dart';
+
+class ExternalPlayer {
+ // 注意:仍需开发 iOS/Linux 设备的外部播放功能。
+ // 在 Windows 设备上,对于其他可能的实现,使用 scheme 的方案没有效果。VLC / PotPlayer 等主流播放器更倾向于使用 CLI 命令。
+ // 可行的 iOS 处理代码,请参见 ios/Runner/AppDelegate.swift 的注释部分。
+ static const platform = MethodChannel('com.predidit.kazumi/intent');
+
+ static Future<bool> launchURLWithMIME(String url, String mimeType) async {
+ try {
+ await platform.invokeMethod(
+ 'openWithMime', <String, String>{'url': url, 'mimeType': mimeType});
+ return true;
+ } on PlatformException catch (e) {
+ KazumiLogger()
+ .log(Level.error, "Failed to open with mime: '${e.message}'.");
+ return false;
+ }
+ }
+
+ static Future<bool> launchURLWithReferer(String url, String referer) async {
+ try {
+ await platform.invokeMethod(
+ 'openWithReferer', <String, String>{'url': url, 'referer': referer});
+ return true;
+ } on PlatformException catch (e) {
+ KazumiLogger()
+ .log(Level.error, "Failed to open with referer: '${e.message}'.");
+ return false;
+ }
+ }
+}
diff --git a/lib/utils/remote.dart b/lib/utils/remote.dart
index af5d8882..ac56522f 100644
--- a/lib/utils/remote.dart
+++ b/lib/utils/remote.dart
@@ -1,90 +1,26 @@
import 'dart:async';
-import 'dart:io';
import 'package:dlna_dart/dlna.dart';
import 'package:flutter/material.dart';
-import 'package:flutter/services.dart';
-import 'package:flutter_modular/flutter_modular.dart';
import 'package:kazumi/bean/dialog/dialog_helper.dart';
import 'package:logger/logger.dart';
import 'package:kazumi/utils/logger.dart';
-import 'package:url_launcher/url_launcher_string.dart';
-
-import '../pages/player/player_controller.dart';
class RemotePlay {
- // 注意:仍需开发 iOS/Linux 设备的远程播放功能。
- // 在 Windows 设备上,对于其他可能的实现,使用 scheme 的方案没有效果。VLC / PotPlayer 等主流播放器更倾向于使用 CLI 命令。
- // 可行的 iOS 处理代码,请参见 ios/Runner/AppDelegate.swift 的注释部分。
-
- static const platform = MethodChannel('com.predidit.kazumi/intent');
-
- Future<void> castVideo(BuildContext context, String referer) async {
+ Future<void> castVideo(String video, String referer) async {
final searcher = DLNAManager();
final dlna = await searcher.start();
- final String video = Modular.get<PlayerController>().videoUrl;
List<Widget> dlnaDevice = [];
- await KazumiDialog.show(builder: (context) {
+ await KazumiDialog.show(builder: (BuildContext context) {
return StatefulBuilder(builder: (context, setState) {
return AlertDialog(
- title: const Text('远程播放'),
+ title: const Text('远程投屏'),
content: SingleChildScrollView(
child: Column(
children: dlnaDevice,
),
),
actions: [
- TextButton(
- onPressed: () async {
- if ((Platform.isAndroid ||
- Platform.isWindows) && referer.isEmpty) {
- if (await _launchURLWithMIME(video, 'video/mp4')) {
- KazumiDialog.dismiss();
- KazumiDialog.showToast(
- message: '尝试唤起外部播放器',
- );
- } else {
- KazumiDialog.showToast(
- message: '唤起外部播放器失败',
- );
- }
- } else if (Platform.isMacOS || Platform.isIOS) {
- if (await _launchURLWithReferer(video, referer)) {
- KazumiDialog.dismiss();
- KazumiDialog.showToast(
- message: '尝试唤起外部播放器',
- );
- } else {
- KazumiDialog.showToast(
- message: '唤起外部播放器失败',
- );
- }
- } else if (Platform.isLinux && referer.isEmpty) {
- KazumiDialog.dismiss();
- if (await canLaunchUrlString(video)) {
- launchUrlString(video);
- KazumiDialog.showToast(
- message: '尝试唤起外部播放器',
- );
- } else {
- KazumiDialog.showToast(
- message: '无法使用外部播放器',
- );
- }
- } else {
- if (referer.isEmpty) {
- KazumiDialog.showToast(
- message: '暂不支持该设备',
- );
- } else {
- KazumiDialog.showToast(
- message: '暂不支持该规则',
- );
- }
- }
- },
- child: const Text('外部播放'),
- ),
const SizedBox(width: 20),
TextButton(
onPressed: () {
@@ -176,28 +112,4 @@ class RemotePlay {
return const Icon(Icons.question_mark);
}
}
-
- Future<bool> _launchURLWithMIME(String url, String mimeType) async {
- try {
- await platform.invokeMethod(
- 'openWithMime', <String, String>{'url': url, 'mimeType': mimeType});
- return true;
- } on PlatformException catch (e) {
- KazumiLogger()
- .log(Level.error, "Failed to open with mime: '${e.message}'.");
- return false;
- }
- }
-
- Future<bool> _launchURLWithReferer(String url, String referer) async {
- try {
- await platform.invokeMethod(
- 'openWithReferer', <String, String>{'url': url, 'referer': referer});
- return true;
- } on PlatformException catch (e) {
- KazumiLogger()
- .log(Level.error, "Failed to open with referer: '${e.message}'.");
- return false;
- }
- }
}