|
| 1 | +// Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:async'; |
| 6 | +import 'dart:ui'; |
| 7 | + |
| 8 | +import 'package:flutter/services.dart'; |
| 9 | +import 'package:flutter/widgets.dart'; |
| 10 | + |
| 11 | +import 'video_player_platform_interface.dart'; |
| 12 | + |
| 13 | +const MethodChannel _channel = MethodChannel('flutter.io/videoPlayer'); |
| 14 | + |
| 15 | +/// An implementation of [VideoPlayerPlatform] that uses method channels. |
| 16 | +class MethodChannelVideoPlayer extends VideoPlayerPlatform { |
| 17 | + @override |
| 18 | + Future<void> init() { |
| 19 | + return _channel.invokeMethod<void>('init'); |
| 20 | + } |
| 21 | + |
| 22 | + @override |
| 23 | + Future<void> dispose(int textureId) { |
| 24 | + return _channel.invokeMethod<void>( |
| 25 | + 'dispose', |
| 26 | + <String, dynamic>{'textureId': textureId}, |
| 27 | + ); |
| 28 | + } |
| 29 | + |
| 30 | + @override |
| 31 | + Future<int> create(DataSource dataSource) async { |
| 32 | + Map<String, dynamic> dataSourceDescription; |
| 33 | + |
| 34 | + switch (dataSource.sourceType) { |
| 35 | + case DataSourceType.asset: |
| 36 | + dataSourceDescription = <String, dynamic>{ |
| 37 | + 'asset': dataSource.asset, |
| 38 | + 'package': dataSource.package, |
| 39 | + }; |
| 40 | + break; |
| 41 | + case DataSourceType.network: |
| 42 | + dataSourceDescription = <String, dynamic>{ |
| 43 | + 'uri': dataSource.uri, |
| 44 | + 'formatHint': _videoFormatStringMap[dataSource.formatHint] |
| 45 | + }; |
| 46 | + break; |
| 47 | + case DataSourceType.file: |
| 48 | + dataSourceDescription = <String, dynamic>{'uri': dataSource.uri}; |
| 49 | + break; |
| 50 | + } |
| 51 | + |
| 52 | + final Map<String, dynamic> response = |
| 53 | + await _channel.invokeMapMethod<String, dynamic>( |
| 54 | + 'create', |
| 55 | + dataSourceDescription, |
| 56 | + ); |
| 57 | + return response['textureId']; |
| 58 | + } |
| 59 | + |
| 60 | + @override |
| 61 | + Future<void> setLooping(int textureId, bool looping) { |
| 62 | + return _channel.invokeMethod<void>( |
| 63 | + 'setLooping', |
| 64 | + <String, dynamic>{ |
| 65 | + 'textureId': textureId, |
| 66 | + 'looping': looping, |
| 67 | + }, |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + @override |
| 72 | + Future<void> play(int textureId) { |
| 73 | + return _channel.invokeMethod<void>( |
| 74 | + 'play', |
| 75 | + <String, dynamic>{'textureId': textureId}, |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + @override |
| 80 | + Future<void> pause(int textureId) { |
| 81 | + return _channel.invokeMethod<void>( |
| 82 | + 'pause', |
| 83 | + <String, dynamic>{'textureId': textureId}, |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + @override |
| 88 | + Future<void> setVolume(int textureId, double volume) { |
| 89 | + return _channel.invokeMethod<void>( |
| 90 | + 'setVolume', |
| 91 | + <String, dynamic>{ |
| 92 | + 'textureId': textureId, |
| 93 | + 'volume': volume, |
| 94 | + }, |
| 95 | + ); |
| 96 | + } |
| 97 | + |
| 98 | + @override |
| 99 | + Future<void> seekTo(int textureId, Duration position) { |
| 100 | + return _channel.invokeMethod<void>( |
| 101 | + 'seekTo', |
| 102 | + <String, dynamic>{ |
| 103 | + 'textureId': textureId, |
| 104 | + 'location': position.inMilliseconds, |
| 105 | + }, |
| 106 | + ); |
| 107 | + } |
| 108 | + |
| 109 | + @override |
| 110 | + Future<Duration> getPosition(int textureId) async { |
| 111 | + return Duration( |
| 112 | + milliseconds: await _channel.invokeMethod<int>( |
| 113 | + 'position', |
| 114 | + <String, dynamic>{'textureId': textureId}, |
| 115 | + ), |
| 116 | + ); |
| 117 | + } |
| 118 | + |
| 119 | + @override |
| 120 | + Stream<VideoEvent> videoEventsFor(int textureId) { |
| 121 | + return _eventChannelFor(textureId) |
| 122 | + .receiveBroadcastStream() |
| 123 | + .map((dynamic event) { |
| 124 | + final Map<dynamic, dynamic> map = event; |
| 125 | + switch (map['event']) { |
| 126 | + case 'initialized': |
| 127 | + return VideoEvent( |
| 128 | + eventType: VideoEventType.initialized, |
| 129 | + duration: Duration(milliseconds: map['duration']), |
| 130 | + size: Size(map['width']?.toDouble() ?? 0.0, |
| 131 | + map['height']?.toDouble() ?? 0.0), |
| 132 | + ); |
| 133 | + case 'completed': |
| 134 | + return VideoEvent( |
| 135 | + eventType: VideoEventType.completed, |
| 136 | + ); |
| 137 | + case 'bufferingUpdate': |
| 138 | + final List<dynamic> values = map['values']; |
| 139 | + |
| 140 | + return VideoEvent( |
| 141 | + buffered: values.map<DurationRange>(_toDurationRange).toList(), |
| 142 | + eventType: VideoEventType.completed, |
| 143 | + ); |
| 144 | + case 'bufferingStart': |
| 145 | + return VideoEvent(eventType: VideoEventType.bufferingStart); |
| 146 | + case 'bufferingEnd': |
| 147 | + return VideoEvent(eventType: VideoEventType.bufferingEnd); |
| 148 | + default: |
| 149 | + return VideoEvent(eventType: VideoEventType.unknown); |
| 150 | + } |
| 151 | + }); |
| 152 | + } |
| 153 | + |
| 154 | + @override |
| 155 | + Widget buildView(int textureId) { |
| 156 | + return Texture(textureId: textureId); |
| 157 | + } |
| 158 | + |
| 159 | + EventChannel _eventChannelFor(int textureId) { |
| 160 | + return EventChannel('flutter.io/videoPlayer/videoEvents$textureId'); |
| 161 | + } |
| 162 | + |
| 163 | + static const Map<VideoFormat, String> _videoFormatStringMap = |
| 164 | + <VideoFormat, String>{ |
| 165 | + VideoFormat.ss: 'ss', |
| 166 | + VideoFormat.hls: 'hls', |
| 167 | + VideoFormat.dash: 'dash', |
| 168 | + VideoFormat.other: 'other', |
| 169 | + }; |
| 170 | + |
| 171 | + DurationRange _toDurationRange(dynamic value) { |
| 172 | + final List<dynamic> pair = value; |
| 173 | + return DurationRange( |
| 174 | + Duration(milliseconds: pair[0]), |
| 175 | + Duration(milliseconds: pair[1]), |
| 176 | + ); |
| 177 | + } |
| 178 | +} |
0 commit comments