Skip to content

Commit

Permalink
support load from asset
Browse files Browse the repository at this point in the history
  • Loading branch information
jakky1 committed Nov 15, 2024
1 parent ae6ae89 commit 777b9dd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
11 changes: 9 additions & 2 deletions lib/video_player_win.dart
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,21 @@ class WinVideoPlayerController extends ValueNotifier<WinVideoPlayerValue> {
VideoPlayerWinPlatform.instance.dispose(textureId);
});

static String getAssetPath(String dataSource) {
File file = File(Platform.resolvedExecutable);
return "${file.parent.path}\\data\\flutter_assets\\$dataSource";
}

WinVideoPlayerController.file(File file, {bool isBridgeMode = false})
: this._(file.path, WinDataSourceType.file, isBridgeMode: isBridgeMode);
WinVideoPlayerController.network(String dataSource,
{bool isBridgeMode = false})
: this._(dataSource, WinDataSourceType.network,
isBridgeMode: isBridgeMode);
WinVideoPlayerController.asset(String dataSource, {String? package})
: this._(dataSource, WinDataSourceType.asset);
WinVideoPlayerController.asset(String dataSource,
{String? package, bool isBridgeMode = false})
: this._(getAssetPath(dataSource), WinDataSourceType.file,
isBridgeMode: isBridgeMode);
WinVideoPlayerController.contentUri(Uri contentUri)
: this._("", WinDataSourceType.contentUri);

Expand Down
27 changes: 14 additions & 13 deletions lib/video_player_win_plugin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class WindowsVideoPlayer extends VideoPlayerPlatform {
/// Creates an instance of a video player and returns its textureId.
@override
Future<int?> create(DataSource dataSource) async {
WinVideoPlayerController? controller;

if (dataSource.sourceType == DataSourceType.file) {
// dataSource.uri is url encoded and has a file:// scheme.
// But if the dataSource.uri original path contains non-ASCII characters,
Expand All @@ -44,26 +46,25 @@ class WindowsVideoPlayer extends VideoPlayerPlatform {
// Without the file:// scheme, the IMFSourceResolver API treats the "%"
// character as a normal string instead of url decoding the path.
var uri = Uri.parse(dataSource.uri!);
var controller = WinVideoPlayerController.file(File(uri.toFilePath()),
controller = WinVideoPlayerController.file(File(uri.toFilePath()),
isBridgeMode: true);
await controller.initialize();
if (controller.textureId_ > 0) {
mControllerMap[controller.textureId_] = controller;
return controller.textureId_;
}
return null;
} else if (dataSource.sourceType == DataSourceType.network) {
var controller =
controller =
WinVideoPlayerController.network(dataSource.uri!, isBridgeMode: true);
await controller.initialize();
if (controller.textureId_ > 0) {
mControllerMap[controller.textureId_] = controller;
return controller.textureId_;
}
} else if (dataSource.sourceType == DataSourceType.asset) {
controller =
WinVideoPlayerController.asset(dataSource.asset!, isBridgeMode: true);
} else {
throw UnimplementedError(
'create() has not been implemented for dataSource type [assets] and [contentUri] in Windows OS');
}

await controller.initialize();
if (controller.textureId_ > 0) {
mControllerMap[controller.textureId_] = controller;
return controller.textureId_;
}
return null;
}

/// Returns a Stream of [VideoEventType]s.
Expand Down

0 comments on commit 777b9dd

Please sign in to comment.