Skip to content

Commit

Permalink
add external playing option for linux
Browse files Browse the repository at this point in the history
-mpv( with header and subtitle support)
-vlc
  • Loading branch information
appdevelpo committed Apr 13, 2024
1 parent 3a0e394 commit edf0eaa
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 7 deletions.
3 changes: 2 additions & 1 deletion lib/controllers/detail_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,8 @@ class DetailPageController extends GetxController {
await launchMobileExternalPlayer(watchData.url, player);
return;
}
await launchDesktopExternalPlayer(watchData.url, player);
await launchDesktopExternalPlayer(watchData.url, player,
watchData.headers ?? {}, watchData.subtitles ?? []);
return;
} catch (e) {
showPlatformSnackbar(
Expand Down
48 changes: 43 additions & 5 deletions lib/utils/external_player.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import 'dart:io';

import 'package:android_intent_plus/android_intent.dart';
import 'package:miru_app/models/index.dart';
import 'package:miru_app/utils/request.dart';
import 'package:path_provider/path_provider.dart';
import 'package:url_launcher/url_launcher_string.dart';

Future<void> launchMobileExternalPlayer(String playUrl, String player) async {
Expand All @@ -19,14 +22,49 @@ Future<void> launchMobileExternalPlayer(String playUrl, String player) async {
}

// desktop
Future<void> launchDesktopExternalPlayer(String playUrl, String player) async {
Future<void> launchDesktopExternalPlayer(
String playUrl,
String player,
Map<String, String> headers,
List<ExtensionBangumiWatchSubtitle> subs) async {
final subLink = subs.map((e) => e.url).toList();
//windows
if (Platform.isWindows) {
switch (player) {
case "vlc":
const vlc = 'C:\\Program Files\\VideoLAN\\VLC\\vlc.exe';
await Process.run(vlc, [playUrl]);
break;
case "potplayer":
await _launchExternalPlayer("potplayer://$playUrl");
break;
}
}
//linux
switch (player) {
case "vlc":
const vlc = 'C:\\Program Files\\VideoLAN\\VLC\\vlc.exe';
await Process.run(vlc, [playUrl]);
await Process.run("vlc", [playUrl]);
break;
case "potplayer":
await _launchExternalPlayer("potplayer://$playUrl");
case "mpv":
final sub = [];
var directory = await getTemporaryDirectory();
for (final s in subs) {
final fileName = Uri.parse(s.url).pathSegments.last;
final response = await dio.get(s.url);
final targetPath = '${directory.path}/$fileName';
await File(targetPath).writeAsString(response.data);
sub.add(targetPath);
}

final uA = headers.remove("User-Agent");
final headerFields =
headers.entries.map((e) => '${e.key}: ${e.value}').join(', ');
await Process.run("mpv", [
playUrl,
'--user-agent=$uA',
if (headerFields.isNotEmpty) '--http-header-fields="$headerFields"',
if (subLink.isNotEmpty) '--sub-files=${sub.join(":")}',
]);
break;
}
}
Expand Down
7 changes: 7 additions & 0 deletions lib/views/pages/settings/settings_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,13 @@ class _SettingsPageState extends State<SettingsPage> {
"Other": "other",
};
}
if (Platform.isLinux) {
return {
"settings.external-player-builtin".i18n: "built-in",
"VLC": "vlc",
"mpv": "mpv",
};
}
return {
"settings.external-player-builtin".i18n: "built-in",
"VLC": "vlc",
Expand Down
2 changes: 1 addition & 1 deletion linux/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ set_target_properties(${BINARY_NAME}
# Generated plugin build rules, which manage building the plugins and adding
# them to the application.
include(flutter/generated_plugins.cmake)

target_link_libraries(${BINARY_NAME} PRIVATE ${MIMALLOC_LIB})

# === Installation ===
# By default, "installing" just makes a relocatable bundle in the build
Expand Down

0 comments on commit edf0eaa

Please sign in to comment.