Skip to content

Commit cbfb02d

Browse files
committed
subtitleFontFile option value can be an http url
1 parent 6a02294 commit cbfb02d

File tree

4 files changed

+42
-13
lines changed

4 files changed

+42
-13
lines changed

example/.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
*.swp
66
.DS_Store
77
.atom/
8+
.build/
89
.buildlog/
910
.history
1011
.svn/
12+
.swiftpm/
1113
migrate_working_dir/
14+
.cxx/
1215

1316
# IntelliJ related
1417
*.iml

lib/fvp.dart

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2022-2024 Wang Bin. All rights reserved.
1+
// Copyright 2022-2025 Wang Bin. All rights reserved.
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

@@ -23,13 +23,13 @@ export 'src/controller.dart';
2323
///
2424
/// 'lowLatency': int. default is 0. reduce network stream latency. 1: for vod. 2: for live stream, may drop frames to ensure the latest content is displayed
2525
///
26-
/// "player": backend player properties of type Map<String, String>. See https://github.com/wang-bin/mdk-sdk/wiki/Player-APIs#void-setpropertyconst-stdstring-key-const-stdstring-value
26+
/// "player": backend player properties of type [Map<String, String>]. See https://github.com/wang-bin/mdk-sdk/wiki/Player-APIs#void-setpropertyconst-stdstring-key-const-stdstring-value
2727
///
28-
/// "global": backend global options of type Map<String, Object>. See https://github.com/wang-bin/mdk-sdk/wiki/Global-Options
28+
/// "global": backend global options of type [Map<String, Object>]. See https://github.com/wang-bin/mdk-sdk/wiki/Global-Options
2929
///
3030
/// "tunnel": android only, default is false. AMediacodec/MediaCodec decoder output to a SurfaceTexture surface directly without OpenGL. Maybe more efficient, but some features are not supported, e.g. HDR tone mapping, less codecs.
3131
///
32-
/// 'subtitleFontFile': default subtitle font file as the fallback. If not set, 'assets/subfont.ttf' will be used, you can add it in pubspec.yaml if you need it.
32+
/// 'subtitleFontFile': default subtitle font file as the fallback, can be an http url. If not set, 'assets/subfont.ttf' will be used, you can add it in pubspec.yaml if you need it.
3333
/// subfont.ttf can be downloaded from https://github.com/mpv-android/mpv-android/raw/master/app/src/main/assets/subfont.ttf
3434
///
3535
/// Example:
@@ -53,6 +53,7 @@ class VideoPlayerRegistrant {
5353
MdkVideoPlayerPlatform.registerVideoPlayerPlatformsWith();
5454
}
5555
}
56+
5657
/*
5758
bool isRegistered() {
5859
return VideoPlayerPlatform.instance.runtimeType == MdkVideoPlayerPlatform;

lib/src/video_player_mdk.dart

+26-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2022-2024 Wang Bin. All rights reserved.
1+
// Copyright 2022-2025 Wang Bin. All rights reserved.
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

@@ -8,6 +8,8 @@ import 'package:flutter/widgets.dart'; //
88
import 'package:flutter/services.dart';
99
import 'package:video_player_platform_interface/video_player_platform_interface.dart';
1010
import 'package:logging/logging.dart';
11+
import 'package:http/http.dart' as http;
12+
import 'package:path_provider/path_provider.dart';
1113
import 'fvp_platform_interface.dart';
1214
import 'extensions.dart';
1315
import 'media_info.dart';
@@ -147,6 +149,7 @@ class MdkVideoPlayerPlatform extends VideoPlayerPlatform {
147149
_tunnel = options["tunnel"];
148150
_playerOpts = options['player'];
149151
_globalOpts = options['global'];
152+
// TODO: _env => putenv
150153
_decoders = options['video.decoders'];
151154
_subtitleFontFile = options['subtitleFontFile'];
152155
}
@@ -196,8 +199,28 @@ class MdkVideoPlayerPlatform extends VideoPlayerPlatform {
196199
// mdk.setGlobalOptions('plugins', 'mdk-braw');
197200
mdk.setGlobalOption("log", "all");
198201
mdk.setGlobalOption('d3d11.sync.cpu', 1);
199-
mdk.setGlobalOption('subtitle.fonts.file',
200-
PlatformEx.assetUri(_subtitleFontFile ?? 'assets/subfont.ttf'));
202+
if (_subtitleFontFile?.startsWith('http') ?? false) {
203+
final fileName = _subtitleFontFile!.split('/').last;
204+
getApplicationCacheDirectory().then((dir) {
205+
final fontPath = '${dir.path}/$fileName';
206+
_log.fine('check font path: $fontPath');
207+
if (File(fontPath).existsSync()) {
208+
mdk.setGlobalOption('subtitle.fonts.file', fontPath);
209+
return;
210+
}
211+
_log.fine('downloading font file: $_subtitleFontFile');
212+
http.get(Uri.parse(_subtitleFontFile!)).then((response) {
213+
if (response.statusCode == 200) {
214+
_log.fine('save font file: $fontPath');
215+
File(fontPath).writeAsBytes(response.bodyBytes).then((_) {
216+
mdk.setGlobalOption('subtitle.fonts.file', fontPath);
217+
});
218+
}
219+
});
220+
});
221+
} else {
222+
mdk.setGlobalOption('subtitle.fonts.file', PlatformEx.assetUri(_subtitleFontFile ?? 'assets/subfont.ttf'));
223+
}
201224
_globalOpts?.forEach((key, value) {
202225
mdk.setGlobalOption(key, value);
203226
});

pubspec.yaml

+8-6
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ topics:
1010
- videoplayer
1111

1212
environment:
13-
sdk: '>=3.0.0 <4.0.0'
14-
flutter: ">=3.0.0"
13+
sdk: ^3.0.0
14+
flutter: ^3.0.0
1515

1616
dependencies:
1717
ffi: ^2.1.0
@@ -22,6 +22,8 @@ dependencies:
2222
plugin_platform_interface: ^2.0.0
2323
video_player: ^2.6.0
2424
video_player_platform_interface: ^6.2.0
25+
path_provider: ^2.1.2
26+
http: ^1.0.0
2527

2628
dev_dependencies:
2729
flutter_test:
@@ -46,7 +48,7 @@ flutter:
4648
# All these are used by the tooling to maintain consistency when
4749
# adding or updating assets for this project.
4850
plugin:
49-
#implements: video_player # flutter 3.27+ can only select 1 implementation
51+
#implements: video_player # endorsed. flutter 3.27+ can only select 1 implementation
5052
platforms:
5153
android:
5254
package: com.mediadevkit.fvp
@@ -70,10 +72,10 @@ flutter:
7072
# - images/a_dot_ham.jpeg
7173
#
7274
# For details regarding assets in packages, see
73-
# https://flutter.dev/assets-and-images/#from-packages
75+
# https://flutter.dev/to/asset-from-package
7476
#
7577
# An image asset can refer to one or more resolution-specific "variants", see
76-
# https://flutter.dev/assets-and-images/#resolution-aware
78+
# https://flutter.dev/to/resolution-aware-images
7779

7880
# To add custom fonts to your plugin package, add a fonts section here,
7981
# in this "flutter" section. Each entry in this list should have a
@@ -93,4 +95,4 @@ flutter:
9395
# weight: 700
9496
#
9597
# For details regarding fonts in packages, see
96-
# https://flutter.dev/custom-fonts/#from-packages
98+
# https://flutter.dev/to/font-from-package

0 commit comments

Comments
 (0)