|
| 1 | +import 'dart:async'; |
| 2 | +import 'dart:html'; |
| 3 | +import 'dart:ui' as ui; |
| 4 | + |
| 5 | +import 'package:flutter/material.dart'; |
| 6 | +import 'package:flutter_web_plugins/flutter_web_plugins.dart'; |
| 7 | +import 'package:video_player_platform_interface/video_player_platform_interface.dart'; |
| 8 | + |
| 9 | +/// The web implementation of [VideoPlayerPlatform]. |
| 10 | +/// |
| 11 | +/// This class implements the `package:video_player` functionality for the web. |
| 12 | +class VideoPlayerPlugin extends VideoPlayerPlatform { |
| 13 | + /// Registers this class as the default instance of [VideoPlayerPlatform]. |
| 14 | + static void registerWith(Registrar registrar) { |
| 15 | + VideoPlayerPlatform.instance = VideoPlayerPlugin(); |
| 16 | + } |
| 17 | + |
| 18 | + Map<int, _VideoPlayer> _videoPlayers = <int, _VideoPlayer>{}; |
| 19 | + |
| 20 | + int _textureCounter = 1; |
| 21 | + |
| 22 | + @override |
| 23 | + Future<void> init() async { |
| 24 | + return _disposeAllPlayers(); |
| 25 | + } |
| 26 | + |
| 27 | + @override |
| 28 | + Future<void> dispose(int textureId) async { |
| 29 | + _videoPlayers[textureId].dispose(); |
| 30 | + _videoPlayers.remove(textureId); |
| 31 | + return null; |
| 32 | + } |
| 33 | + |
| 34 | + void _disposeAllPlayers() { |
| 35 | + _videoPlayers.values |
| 36 | + .forEach((_VideoPlayer videoPlayer) => videoPlayer.dispose()); |
| 37 | + _videoPlayers.clear(); |
| 38 | + } |
| 39 | + |
| 40 | + @override |
| 41 | + Future<int> create(DataSource dataSource) async { |
| 42 | + final int textureId = _textureCounter; |
| 43 | + _textureCounter++; |
| 44 | + |
| 45 | + final _VideoPlayer player = _VideoPlayer( |
| 46 | + uri: Uri.parse(dataSource.uri), |
| 47 | + textureId: textureId, |
| 48 | + ); |
| 49 | + |
| 50 | + player.initialize(); |
| 51 | + |
| 52 | + _videoPlayers[textureId] = player; |
| 53 | + return textureId; |
| 54 | + } |
| 55 | + |
| 56 | + @override |
| 57 | + Future<void> setLooping(int textureId, bool looping) async { |
| 58 | + return _videoPlayers[textureId].setLooping(looping); |
| 59 | + } |
| 60 | + |
| 61 | + @override |
| 62 | + Future<void> play(int textureId) async { |
| 63 | + return _videoPlayers[textureId].play(); |
| 64 | + } |
| 65 | + |
| 66 | + @override |
| 67 | + Future<void> pause(int textureId) async { |
| 68 | + return _videoPlayers[textureId].pause(); |
| 69 | + } |
| 70 | + |
| 71 | + @override |
| 72 | + Future<void> setVolume(int textureId, double volume) async { |
| 73 | + return _videoPlayers[textureId].setVolume(volume); |
| 74 | + } |
| 75 | + |
| 76 | + @override |
| 77 | + Future<void> seekTo(int textureId, Duration position) async { |
| 78 | + return _videoPlayers[textureId].seekTo(position); |
| 79 | + } |
| 80 | + |
| 81 | + @override |
| 82 | + Future<Duration> getPosition(int textureId) async { |
| 83 | + _videoPlayers[textureId].sendBufferingUpdate(); |
| 84 | + return _videoPlayers[textureId].getPosition(); |
| 85 | + } |
| 86 | + |
| 87 | + @override |
| 88 | + Stream<VideoEvent> videoEventsFor(int textureId) { |
| 89 | + return _videoPlayers[textureId].eventController.stream; |
| 90 | + } |
| 91 | + |
| 92 | + @override |
| 93 | + Widget buildView(int textureId) { |
| 94 | + return HtmlElementView(viewType: 'videoPlayer-$textureId'); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +class _VideoPlayer { |
| 99 | + _VideoPlayer({this.uri, this.textureId}); |
| 100 | + |
| 101 | + final StreamController<VideoEvent> eventController = |
| 102 | + StreamController<VideoEvent>(); |
| 103 | + |
| 104 | + final Uri uri; |
| 105 | + final int textureId; |
| 106 | + VideoElement videoElement; |
| 107 | + bool isInitialized = false; |
| 108 | + |
| 109 | + void initialize() { |
| 110 | + videoElement = VideoElement() |
| 111 | + ..src = uri.toString() |
| 112 | + ..autoplay = false |
| 113 | + ..controls = false |
| 114 | + ..style.border = 'none'; |
| 115 | + |
| 116 | + // TODO(hterkelsen): Use initialization parameters once they are available |
| 117 | + // ignore: undefined_prefixed_name |
| 118 | + ui.platformViewRegistry.registerViewFactory( |
| 119 | + 'videoPlayer-$textureId', (int viewId) => videoElement); |
| 120 | + |
| 121 | + videoElement.onCanPlay.listen((dynamic _) { |
| 122 | + if (!isInitialized) { |
| 123 | + isInitialized = true; |
| 124 | + sendInitialized(); |
| 125 | + } |
| 126 | + }); |
| 127 | + videoElement.onError.listen((dynamic error) { |
| 128 | + eventController.addError(error); |
| 129 | + }); |
| 130 | + videoElement.onEnded.listen((dynamic _) { |
| 131 | + eventController.add(VideoEvent(eventType: VideoEventType.completed)); |
| 132 | + }); |
| 133 | + } |
| 134 | + |
| 135 | + void sendBufferingUpdate() { |
| 136 | + eventController.add(VideoEvent( |
| 137 | + buffered: _toDurationRange(videoElement.buffered), |
| 138 | + eventType: VideoEventType.bufferingUpdate, |
| 139 | + )); |
| 140 | + } |
| 141 | + |
| 142 | + void play() { |
| 143 | + videoElement.play(); |
| 144 | + } |
| 145 | + |
| 146 | + void pause() { |
| 147 | + videoElement.pause(); |
| 148 | + } |
| 149 | + |
| 150 | + void setLooping(bool value) { |
| 151 | + videoElement.loop = value; |
| 152 | + } |
| 153 | + |
| 154 | + void setVolume(double value) { |
| 155 | + videoElement.volume = value; |
| 156 | + } |
| 157 | + |
| 158 | + void seekTo(Duration position) { |
| 159 | + videoElement.currentTime = position.inMilliseconds.toDouble() / 1000; |
| 160 | + } |
| 161 | + |
| 162 | + Duration getPosition() { |
| 163 | + return Duration(milliseconds: (videoElement.currentTime * 1000).round()); |
| 164 | + } |
| 165 | + |
| 166 | + void sendInitialized() { |
| 167 | + eventController.add( |
| 168 | + VideoEvent( |
| 169 | + eventType: VideoEventType.initialized, |
| 170 | + duration: Duration( |
| 171 | + milliseconds: (videoElement.duration * 1000).round(), |
| 172 | + ), |
| 173 | + size: Size( |
| 174 | + videoElement.videoWidth.toDouble() ?? 0.0, |
| 175 | + videoElement.videoHeight.toDouble() ?? 0.0, |
| 176 | + ), |
| 177 | + ), |
| 178 | + ); |
| 179 | + } |
| 180 | + |
| 181 | + void dispose() { |
| 182 | + videoElement.removeAttribute('src'); |
| 183 | + videoElement.load(); |
| 184 | + } |
| 185 | + |
| 186 | + List<DurationRange> _toDurationRange(TimeRanges buffered) { |
| 187 | + final List<DurationRange> durationRange = <DurationRange>[]; |
| 188 | + for (int i = 0; i < buffered.length; i++) { |
| 189 | + durationRange.add(DurationRange( |
| 190 | + Duration(milliseconds: (buffered.start(i) * 1000).round()), |
| 191 | + Duration(milliseconds: (buffered.end(i) * 1000).round()), |
| 192 | + )); |
| 193 | + } |
| 194 | + return durationRange; |
| 195 | + } |
| 196 | +} |
0 commit comments