This repository was archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[video_player] [WIP] Add web implementation #2041
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| import 'dart:async'; | ||
| import 'dart:html'; | ||
| import 'dart:ui' as ui; | ||
|
|
||
| import 'package:flutter/services.dart'; | ||
| import 'package:flutter_web_plugins/flutter_web_plugins.dart'; | ||
|
|
||
| class VideoPlayer { | ||
| VideoPlayer({this.uri, this.eventChannel, this.textureId}); | ||
|
|
||
| final PluginEventChannel<Map<String, dynamic>> eventChannel; | ||
| final StreamController<Map<String, dynamic>> controller = | ||
| StreamController<Map<String, dynamic>>(); | ||
|
|
||
| final Uri uri; | ||
| final int textureId; | ||
| VideoElement videoElement; | ||
| bool isInitialized = false; | ||
|
|
||
| Map<String, int> setupVideoPlayer( | ||
| PluginEventChannel<Map<String, dynamic>> eventChannel) { | ||
| eventChannel.controller = controller; | ||
| videoElement = VideoElement() | ||
| ..src = uri.toString() | ||
| ..autoplay = false | ||
| ..controls = false | ||
| ..style.border = 'none'; | ||
|
|
||
| ui.platformViewRegistry.registerViewFactory( | ||
| textureId.toString(), (int viewId) => videoElement); | ||
|
|
||
| videoElement.onCanPlay.listen((dynamic _) { | ||
| if (!isInitialized) { | ||
| isInitialized = true; | ||
| sendInitialized(); | ||
| } | ||
| }); | ||
| videoElement.onError.listen((dynamic error) { | ||
| controller.addError(error); | ||
| }); | ||
| videoElement.onEnded.listen((dynamic _) { | ||
| final Map<String, String> event = <String, String>{"event": "completed"}; | ||
| controller.add(event); | ||
| }); | ||
|
|
||
| final Map<String, int> reply = <String, int>{"textureId": textureId}; | ||
| return reply; | ||
| } | ||
|
|
||
| void sendBufferingUpdate() { | ||
| // TODO: convert TimeRanges | ||
| final Map<String, dynamic> event = <String, dynamic>{ | ||
| "event": "bufferingUpdate", | ||
| "values": videoElement.buffered, | ||
| }; | ||
| controller.add(event); | ||
| } | ||
|
|
||
| void play() { | ||
| videoElement.play(); | ||
| } | ||
|
|
||
| void pause() { | ||
| videoElement.pause(); | ||
| } | ||
|
|
||
| void setLooping(bool value) { | ||
| videoElement.loop = value; | ||
| } | ||
|
|
||
| void setVolume(double value) { | ||
| videoElement.volume = value; | ||
| } | ||
|
|
||
| void seekTo(int location) { | ||
| videoElement.currentTime = location.toDouble() / 1000; | ||
| } | ||
|
|
||
| int getPosition() { | ||
| final int position = (videoElement.currentTime * 1000).round(); | ||
| return position; | ||
| } | ||
|
|
||
| void sendInitialized() { | ||
| final Map<String, dynamic> event = <String, dynamic>{ | ||
| 'event': 'initialized', | ||
| 'duration': videoElement.duration * 1000, | ||
| 'width': videoElement.videoWidth, | ||
| 'height': videoElement.videoHeight, | ||
| }; | ||
| controller.add(event); | ||
| } | ||
|
|
||
| void dispose() { | ||
| videoElement.removeAttribute('src'); | ||
| videoElement.load(); | ||
| } | ||
| } | ||
|
|
||
| class VideoPlayerPlugin { | ||
| VideoPlayerPlugin(this._registrar); | ||
|
|
||
| static void registerWith(Registrar registrar) { | ||
| final MethodChannel channel = MethodChannel('flutter.io/videoPlayer', | ||
| const StandardMethodCodec(), registrar.messenger); | ||
| final VideoPlayerPlugin instance = VideoPlayerPlugin(registrar); | ||
| channel.setMethodCallHandler(instance.handleMethodCall); | ||
| } | ||
|
|
||
| Map<int, VideoPlayer> videoPlayers = <int, VideoPlayer>{}; | ||
| Registrar _registrar; | ||
|
|
||
| int textureCounter = 1; | ||
|
|
||
| Future<dynamic> handleMethodCall(MethodCall call) async { | ||
| switch (call.method) { | ||
| case "init": | ||
| // TODO: gracefully handle multiple calls to init | ||
| // disposeAllPlayers(); | ||
| break; | ||
| case "create": | ||
| final int textureId = textureCounter; | ||
| textureCounter++; | ||
|
|
||
| final PluginEventChannel<Map<String, dynamic>> eventChannel = | ||
| PluginEventChannel<Map<String, dynamic>>( | ||
| 'flutter.io/videoPlayer/videoEvents$textureId', | ||
| const StandardMethodCodec(), | ||
| _registrar.messenger); | ||
|
|
||
| final VideoPlayer player = VideoPlayer( | ||
| uri: Uri.parse(call.arguments['uri']), | ||
| eventChannel: eventChannel, | ||
| textureId: textureId, | ||
| ); | ||
|
|
||
| final Map<String, int> reply = player.setupVideoPlayer(eventChannel); | ||
|
|
||
| videoPlayers[textureId] = player; | ||
| return reply; | ||
|
|
||
| default: | ||
| final int textureId = call.arguments["textureId"]; | ||
|
|
||
| final VideoPlayer player = videoPlayers[textureId]; | ||
| if (player == null) { | ||
| throw Exception( | ||
| "No video player associated with texture id $textureId"); | ||
| } | ||
|
|
||
| return _onMethodCall(call, textureId, player); | ||
| } | ||
| } | ||
|
|
||
| void disposeAllPlayers() { | ||
| videoPlayers.forEach((_, VideoPlayer videoPlayer) => videoPlayer.dispose()); | ||
| videoPlayers.clear(); | ||
| } | ||
|
|
||
| dynamic _onMethodCall(MethodCall call, int textureId, VideoPlayer player) { | ||
| switch (call.method) { | ||
| case "setLooping": | ||
| player.setLooping(call.arguments["looping"]); | ||
| return null; | ||
| case "setVolume": | ||
| player.setVolume(call.arguments["volume"]); | ||
| return null; | ||
| case "play": | ||
| player.play(); | ||
| return null; | ||
| case "pause": | ||
| player.pause(); | ||
| return null; | ||
| case "seekTo": | ||
| player.seekTo(call.arguments["location"]); | ||
| return null; | ||
| case "position": | ||
| player.sendBufferingUpdate(); | ||
| return player.getPosition(); | ||
| case "dispose": | ||
| player.dispose(); | ||
| videoPlayers.remove(textureId); | ||
| return null; | ||
| default: | ||
| throw UnimplementedError(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can't land this before next stable release as the plugin won't compile with the stable tool.