Skip to content

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

add native external player implements

Predidit14d0a0151110
diff --git a/.vscode/settings.json b/.vscode/settings.json
index b88620d1..3add74f3 100644
--- a/.vscode/settings.json
+++ b/.vscode/settings.json
@@ -1,6 +1,81 @@
{
"cmake.ignoreCMakeListsMissing": true,
"files.associations": {
- "xiosbase": "cpp"
+ "xiosbase": "cpp",
+ "utility": "cpp",
+ "xstring": "cpp",
+ "xtree": "cpp",
+ "algorithm": "cpp",
+ "any": "cpp",
+ "array": "cpp",
+ "atomic": "cpp",
+ "bit": "cpp",
+ "cctype": "cpp",
+ "charconv": "cpp",
+ "chrono": "cpp",
+ "cinttypes": "cpp",
+ "clocale": "cpp",
+ "cmath": "cpp",
+ "codecvt": "cpp",
+ "compare": "cpp",
+ "concepts": "cpp",
+ "condition_variable": "cpp",
+ "coroutine": "cpp",
+ "cstddef": "cpp",
+ "cstdint": "cpp",
+ "cstdio": "cpp",
+ "cstdlib": "cpp",
+ "cstring": "cpp",
+ "ctime": "cpp",
+ "cwchar": "cpp",
+ "exception": "cpp",
+ "filesystem": "cpp",
+ "format": "cpp",
+ "forward_list": "cpp",
+ "functional": "cpp",
+ "future": "cpp",
+ "initializer_list": "cpp",
+ "iomanip": "cpp",
+ "ios": "cpp",
+ "iosfwd": "cpp",
+ "iostream": "cpp",
+ "istream": "cpp",
+ "iterator": "cpp",
+ "limits": "cpp",
+ "list": "cpp",
+ "locale": "cpp",
+ "map": "cpp",
+ "memory": "cpp",
+ "mutex": "cpp",
+ "new": "cpp",
+ "optional": "cpp",
+ "ostream": "cpp",
+ "ratio": "cpp",
+ "set": "cpp",
+ "sstream": "cpp",
+ "stdexcept": "cpp",
+ "stop_token": "cpp",
+ "streambuf": "cpp",
+ "string": "cpp",
+ "system_error": "cpp",
+ "thread": "cpp",
+ "tuple": "cpp",
+ "type_traits": "cpp",
+ "typeinfo": "cpp",
+ "unordered_map": "cpp",
+ "variant": "cpp",
+ "vector": "cpp",
+ "xfacet": "cpp",
+ "xhash": "cpp",
+ "xlocale": "cpp",
+ "xlocbuf": "cpp",
+ "xlocinfo": "cpp",
+ "xlocmes": "cpp",
+ "xlocmon": "cpp",
+ "xlocnum": "cpp",
+ "xloctime": "cpp",
+ "xmemory": "cpp",
+ "xtr1common": "cpp",
+ "xutility": "cpp"
}
}
\ No newline at end of file
diff --git a/lib/utils/remote.dart b/lib/utils/remote.dart
index 2df6211b..f11e16f1 100644
--- a/lib/utils/remote.dart
+++ b/lib/utils/remote.dart
@@ -6,21 +6,22 @@ import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_modular/flutter_modular.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
-import 'package:url_launcher/url_launcher_string.dart';
import 'package:logger/logger.dart';
import 'package:kazumi/utils/logger.dart';
import '../pages/player/player_controller.dart';
class RemotePlay {
-
- // 注意:仍需开发非 Android 设备的远程播放功能。
- // 外部播放使用 MethodChannel 方法实现,而这只在 Android 设备中有效。
- // 对于 Windows 设备,使用了 url_launcher 以支持浏览器的播放。
- // 在 Windows 设备上,使用 scheme 的方案没有效果。VLC / PotPlayer 等主流播放器更倾向于使用 CLI 命令。
+ // 注意:仍需开发 iOS/macOS/Linux 设备的远程播放功能。
+ // 在 Windows 设备上,对于其他可能的实现,使用 scheme 的方案没有效果。VLC / PotPlayer 等主流播放器更倾向于使用 CLI 命令。
// 而对于 iOS / Mac 设备,由于没有设备,无法进行开发与验证。
// 可行的 iOS / Mac 处理代码,请参见 ios/Runner/AppDelegate.swift 的注释部分。
+ // 2024/09/28
+ // Windows 外部播放器调用方法更新。
+ // 主要问题在于 win32 API 中 shellExecuteEx 需要在网络资源全部缓存到本地后才能进行操作。这导致及其缓慢的启动时间。
+ // 这已经是我目前可以找到的最通用的方法,基于 cli 的方法缺乏通用性,不是我们想要的。
+
static const platform = MethodChannel('com.predidit.kazumi/intent');
castVideo(BuildContext context) async {
@@ -29,115 +30,143 @@ class RemotePlay {
final String video = Modular.get<PlayerController>().videoUrl;
List<Widget> dlnaDevice = [];
SmartDialog.show(
- useAnimation: false,
- builder: (context) {
- return StatefulBuilder(builder: (context, setState) {
- return AlertDialog(
- title: const Text('远程播放'),
- content: SingleChildScrollView(
- child: Column(
- children: dlnaDevice,
- ),
- ),
- actions: [
- TextButton(
- onPressed: () async {
- searcher.stop();
- if (Platform.isAndroid) {
- if (await _launchURLWithMIME(video, 'video/mp4')) {
- SmartDialog.dismiss();
- SmartDialog.showToast(
- '尝试唤起外部播放器',displayType: SmartToastType.onlyRefresh);
- }
- else
- {
- SmartDialog.showToast(
- '无法使用外部播放器',displayType: SmartToastType.onlyRefresh);
- }
- }
- else if (Platform.isWindows) {
- SmartDialog.dismiss();
- if (await canLaunchUrlString(video) == true) {
- launchUrlString(video);
- SmartDialog.showToast(
- '尝试唤起外部播放器',displayType: SmartToastType.onlyRefresh);
- }
- else {
- SmartDialog.showToast(
- '无法使用外部播放器',displayType: SmartToastType.onlyRefresh);
- }
- }
- else {
- SmartDialog.showToast(
- '暂不支持该设备',
- displayType: SmartToastType.onlyRefresh
- );
- }
- },
- child: const Text('外部播放'),
- ),
- const SizedBox(width: 20),
- TextButton(
- onPressed: () {
- searcher.stop();
- SmartDialog.dismiss();
- },
- child: Text(
- '退出',
- style: TextStyle(color: Theme.of(context).colorScheme.outline),
+ useAnimation: false,
+ builder: (context) {
+ return StatefulBuilder(builder: (context, setState) {
+ return AlertDialog(
+ title: const Text('远程播放'),
+ content: SingleChildScrollView(
+ child: Column(
+ children: dlnaDevice,
+ ),
),
- ),
- TextButton(
- onPressed: () {
- setState(() {});
- SmartDialog.showToast(
- '开始搜索',
- displayType: SmartToastType.onlyRefresh
- );
- dlna.devices.stream.listen((deviceList) {
- dlnaDevice = [];
- deviceList.forEach((key, value) async {
- debugPrint('Key: $key');
- debugPrint(
- 'Value: ${value.info.friendlyName} ${value.info.deviceType} ${value.info.URLBase}');
- setState(() {
- dlnaDevice.add(ListTile(
- leading: _deviceUPnPIcon(
- value.info.deviceType.split(':')[3]),
- title: Text(value.info.friendlyName),
- subtitle: Text(value.info.deviceType.split(':')[3]),
- onTap: () {
- try {
- SmartDialog.showToast(
- '尝试投屏至 ${value.info.friendlyName}',
- displayType: SmartToastType.onlyRefresh);
- DLNADevice(value.info).setUrl(video);
- DLNADevice(value.info).play();
- }
- catch (e) {
- KazumiLogger().log(Level.error, 'DLNA Error: $e');
- SmartDialog.showNotify(msg: 'DLNA 异常: $e \n尝试重新进入 DLNA 投屏或切换设备', notifyType: NotifyType.alert);
- }
- }));
+ actions: [
+ TextButton(
+ onPressed: () async {
+ searcher.stop();
+ if (Platform.isAndroid) {
+ if (await _launchURLWithMIME(video, 'video/mp4')) {
+ SmartDialog.dismiss();
+ SmartDialog.showToast('尝试唤起外部播放器',
+ displayType: SmartToastType.onlyRefresh);
+ } else {
+ SmartDialog.showToast('唤起外部播放器失败',
+ displayType: SmartToastType.onlyRefresh);
+ }
+ } else if (Platform.isWindows) {
+ SmartDialog.dismiss();
+ SmartDialog.show(
+ useAnimation: false,
+ builder: (context) {
+ return AlertDialog(
+ title: const Text('实验性功能'),
+ content: const Text(
+ '调用系统当前m3u8默认打开方式进行播放。外部播放器调用在windows上仍处于实验阶段, 存在缺陷。包括但不限于无法调用播放器、无法播放等。此外还需要一定的缓存时间进行播放器初始化。'),
+ actions: [
+ TextButton(
+ onPressed: () => SmartDialog.dismiss(),
+ child: Text(
+ '取消',
+ style: TextStyle(
+ color: Theme.of(context)
+ .colorScheme
+ .outline),
+ ),
+ ),
+ TextButton(
+ onPressed: () {
+ SmartDialog.dismiss();
+ SmartDialog.showToast(
+ '尝试唤起外部播放器, 这可能需要一些时间',
+ displayType:
+ SmartToastType.onlyRefresh);
+ _launchURLWithMIME(video, 'video/mp4')
+ .then((value) {
+ if (!value) {
+ SmartDialog.showToast('唤起外部播放器失败',
+ displayType:
+ SmartToastType.onlyRefresh);
+ }
+ });
+ },
+ child: const Text('继续'),
+ )
+ ],
+ );
+ });
+ } else {
+ SmartDialog.showToast('暂不支持该设备',
+ displayType: SmartToastType.onlyRefresh);
+ }
+ },
+ child: const Text('外部播放'),
+ ),
+ const SizedBox(width: 20),
+ TextButton(
+ onPressed: () {
+ searcher.stop();
+ SmartDialog.dismiss();
+ },
+ child: Text(
+ '退出',
+ style:
+ TextStyle(color: Theme.of(context).colorScheme.outline),
+ ),
+ ),
+ TextButton(
+ onPressed: () {
+ setState(() {});
+ SmartDialog.showToast('开始搜索',
+ displayType: SmartToastType.onlyRefresh);
+ dlna.devices.stream.listen((deviceList) {
+ dlnaDevice = [];
+ deviceList.forEach((key, value) async {
+ debugPrint('Key: $key');
+ debugPrint(
+ 'Value: ${value.info.friendlyName} ${value.info.deviceType} ${value.info.URLBase}');
+ setState(() {
+ dlnaDevice.add(ListTile(
+ leading: _deviceUPnPIcon(
+ value.info.deviceType.split(':')[3]),
+ title: Text(value.info.friendlyName),
+ subtitle:
+ Text(value.info.deviceType.split(':')[3]),
+ onTap: () {
+ try {
+ SmartDialog.showToast(
+ '尝试投屏至 ${value.info.friendlyName}',
+ displayType:
+ SmartToastType.onlyRefresh);
+ DLNADevice(value.info).setUrl(video);
+ DLNADevice(value.info).play();
+ } catch (e) {
+ KazumiLogger()
+ .log(Level.error, 'DLNA Error: $e');
+ SmartDialog.showNotify(
+ msg:
+ 'DLNA 异常: $e \n尝试重新进入 DLNA 投屏或切换设备',
+ notifyType: NotifyType.alert);
+ }
+ }));
+ });
});
});
- });
- Timer(const Duration(seconds: 30), () {
- SmartDialog.showToast(
- '已搜索30s,若未发现设备请尝试重新进入 DLNA 投屏',
- displayType: SmartToastType.onlyRefresh,
- );
- });
- },
- child: Text(
- '搜索',
- style:
- TextStyle(color: Theme.of(context).colorScheme.outline),
- )),
- ],
- );
- });
- });
+ Timer(const Duration(seconds: 30), () {
+ SmartDialog.showToast(
+ '已搜索30s,若未发现设备请尝试重新进入 DLNA 投屏',
+ displayType: SmartToastType.onlyRefresh,
+ );
+ });
+ },
+ child: Text(
+ '搜索',
+ style: TextStyle(
+ color: Theme.of(context).colorScheme.outline),
+ )),
+ ],
+ );
+ });
+ });
}
Icon _deviceUPnPIcon(String deviceType) {
@@ -169,13 +198,12 @@ class RemotePlay {
Future<bool> _launchURLWithMIME(String url, String mimeType) async {
try {
- await platform.invokeMethod('openWithMime', <String, String>{
- 'url': url,
- 'mimeType': mimeType
- });
+ 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}'.");
+ KazumiLogger()
+ .log(Level.error, "Failed to open with mime: '${e.message}'.");
return false;
}
}
diff --git a/windows/runner/CMakeLists.txt b/windows/runner/CMakeLists.txt
index a1e7c661..6e81fe50 100644
--- a/windows/runner/CMakeLists.txt
+++ b/windows/runner/CMakeLists.txt
@@ -10,6 +10,7 @@ add_executable(${BINARY_NAME} WIN32
"flutter_window.cpp"
"main.cpp"
"utils.cpp"
+ "external_player_utils.cpp"
"fullscreen_utils.cpp"
"win32_window.cpp"
"${FLUTTER_MANAGED_DIR}/generated_plugin_registrant.cc"
diff --git a/windows/runner/external_player_utils.cpp b/windows/runner/external_player_utils.cpp
new file mode 100644
index 00000000..06cbaa12
--- /dev/null
+++ b/windows/runner/external_player_utils.cpp
@@ -0,0 +1,32 @@
+// This file is a part of Kazumi
+// (https://github.com/Predidit/Kazumi).
+//
+// Copyright © 2024 Predidit
+// All rights reserved.
+// Use of this source code is governed by GPLv3 license that can be found in the
+// LICENSE file.
+
+#include <windows.h>
+#include <cstdio>
+#include <string>
+#include <iostream>
+#include "external_player_utils.h"
+
+void ExternalPlayerUtils::OpenWithPlayer(const char* url) {
+ // Convert the URL from char* to wchar_t*
+ int urlLength = MultiByteToWideChar(CP_ACP, 0, url, -1, NULL, 0);
+ wchar_t* wideUrl = new wchar_t[urlLength];
+ MultiByteToWideChar(CP_ACP, 0, url, -1, wideUrl, urlLength);
+
+ SHELLEXECUTEINFO execInfo = {0};
+ execInfo.cbSize = sizeof(SHELLEXECUTEINFO);
+ execInfo.fMask = SEE_MASK_CLASSNAME;
+ execInfo.lpVerb = L"open";
+ execInfo.lpFile = wideUrl;
+ execInfo.lpClass = L".m3u8";
+ execInfo.nShow = SW_SHOWNORMAL;
+
+ ShellExecuteEx(&execInfo);
+
+ delete[] wideUrl;
+}
\ No newline at end of file
diff --git a/windows/runner/external_player_utils.h b/windows/runner/external_player_utils.h
new file mode 100644
index 00000000..4f21e0bf
--- /dev/null
+++ b/windows/runner/external_player_utils.h
@@ -0,0 +1,21 @@
+// This file is a part of Kazumi
+// (https://github.com/Predidit/Kazumi).
+//
+// Copyright © 2024 Predidit
+// All rights reserved.
+// Use of this source code is governed by GPLv3 license that can be found in the
+// LICENSE file.
+
+#ifndef EXTERNAL_PLAYER_UTILS_H_
+#define EXTERNAL_PLAYER_UTILS_H_
+
+#include <cstdint>
+
+#include <Windows.h>
+
+class ExternalPlayerUtils {
+ public:
+ static void OpenWithPlayer(const char* url);
+};
+
+#endif // EXTERNAL_PLAYER_UTILS_H_
diff --git a/windows/runner/flutter_window.cpp b/windows/runner/flutter_window.cpp
index 10c825c6..7449ce8a 100644
--- a/windows/runner/flutter_window.cpp
+++ b/windows/runner/flutter_window.cpp
@@ -1,5 +1,6 @@
#include "flutter_window.h"
#include "fullscreen_utils.h"
+#include "external_player_utils.h"
#include <optional>
#include <flutter/method_channel.h>
@@ -92,6 +93,20 @@ void FlutterWindow::RegisterIntentChannel() {
} else if (call.method_name().compare("exitFullscreen") == 0) {
FullscreenUtils::ExitNativeFullscreen(GetHandle());
result->Success();
+ } else if (call.method_name().compare("openWithMime") == 0) {
+ const auto* arguments = std::get_if<flutter::EncodableMap>(call.arguments());
+ if (arguments) {
+ auto url_it = arguments->find(flutter::EncodableValue("url"));
+ if (url_it != arguments->end()) {
+ const std::string& url = std::get<std::string>(url_it->second);
+ ExternalPlayerUtils::OpenWithPlayer(url.c_str());
+ result->Success();
+ } else {
+ result->Error("InvalidArguments", "Missing 'url' argument");
+ }
+ } else {
+ result->Error("InvalidArguments", "Arguments are not a map");
+ }
} else {
result->NotImplemented();
}