|
| 1 | +//MIT License |
| 2 | +// |
| 3 | +//Copyright (c) [2019] [Befovy] |
| 4 | +// |
| 5 | +//Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +//of this software and associated documentation files (the "Software"), to deal |
| 7 | +//in the Software without restriction, including without limitation the rights |
| 8 | +//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +//copies of the Software, and to permit persons to whom the Software is |
| 10 | +//furnished to do so, subject to the following conditions: |
| 11 | +// |
| 12 | +//The above copyright notice and this permission notice shall be included in all |
| 13 | +//copies or substantial portions of the Software. |
| 14 | +// |
| 15 | +//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +//SOFTWARE. |
| 22 | + |
| 23 | +import 'dart:async'; |
| 24 | +import 'dart:math'; |
| 25 | + |
| 26 | +import 'package:fijkplayer/fijkplayer.dart'; |
| 27 | +import 'package:flutter/material.dart'; |
| 28 | +import 'package:flutter_screenutil/flutter_screenutil.dart'; |
| 29 | +import 'package:open_eye/utils/log_utils.dart'; |
| 30 | + |
| 31 | +/// Default builder generate default [FijkPanel] UI |
| 32 | +Widget simpleFijkPanelBuilder(FijkPlayer player, FijkData data, |
| 33 | + BuildContext context, Size viewSize, Rect texturePos) { |
| 34 | + return _DefaultFijkPanel( |
| 35 | + player: player, |
| 36 | + buildContext: context, |
| 37 | + viewSize: viewSize, |
| 38 | + texturePos: texturePos); |
| 39 | +} |
| 40 | + |
| 41 | +/// Default Panel Widget |
| 42 | +class _DefaultFijkPanel extends StatefulWidget { |
| 43 | + final FijkPlayer player; |
| 44 | + final BuildContext buildContext; |
| 45 | + final Size viewSize; |
| 46 | + final Rect texturePos; |
| 47 | + |
| 48 | + const _DefaultFijkPanel({ |
| 49 | + required this.player, |
| 50 | + required this.buildContext, |
| 51 | + required this.viewSize, |
| 52 | + required this.texturePos, |
| 53 | + }); |
| 54 | + |
| 55 | + @override |
| 56 | + _DefaultFijkPanelState createState() => _DefaultFijkPanelState(); |
| 57 | +} |
| 58 | + |
| 59 | +String _duration2String(Duration duration) { |
| 60 | + if (duration.inMilliseconds < 0) return "-: negtive"; |
| 61 | + |
| 62 | + String twoDigits(int n) { |
| 63 | + if (n >= 10) return "$n"; |
| 64 | + return "0$n"; |
| 65 | + } |
| 66 | + |
| 67 | + String twoDigitMinutes = twoDigits(duration.inMinutes.remainder(60)); |
| 68 | + String twoDigitSeconds = twoDigits(duration.inSeconds.remainder(60)); |
| 69 | + int inHours = duration.inHours; |
| 70 | + return inHours > 0 |
| 71 | + ? "$inHours:$twoDigitMinutes:$twoDigitSeconds" |
| 72 | + : "$twoDigitMinutes:$twoDigitSeconds"; |
| 73 | +} |
| 74 | + |
| 75 | +class _DefaultFijkPanelState extends State<_DefaultFijkPanel> { |
| 76 | + FijkPlayer get player => widget.player; |
| 77 | + |
| 78 | + Duration _duration = const Duration(); |
| 79 | + Duration _currentPos = const Duration(); |
| 80 | + Duration _bufferPos = const Duration(); |
| 81 | + |
| 82 | + bool _playing = false; |
| 83 | + bool _prepared = false; |
| 84 | + String? _exception; |
| 85 | + |
| 86 | + // bool _buffering = false; |
| 87 | + |
| 88 | + double _seekPos = -1.0; |
| 89 | + |
| 90 | + StreamSubscription? _currentPosSubs; |
| 91 | + |
| 92 | + StreamSubscription? _bufferPosSubs; |
| 93 | + |
| 94 | + //StreamSubscription _bufferingSubs; |
| 95 | + |
| 96 | + Timer? _hideTimer; |
| 97 | + bool _hideStuff = true; |
| 98 | + |
| 99 | + double _volume = 1.0; |
| 100 | + |
| 101 | + final barHeight = 40.0; |
| 102 | + |
| 103 | + @override |
| 104 | + void initState() { |
| 105 | + super.initState(); |
| 106 | + |
| 107 | + _duration = player.value.duration; |
| 108 | + _currentPos = player.currentPos; |
| 109 | + _bufferPos = player.bufferPos; |
| 110 | + _prepared = player.state.index >= FijkState.prepared.index; |
| 111 | + _playing = player.state == FijkState.started; |
| 112 | + _exception = player.value.exception.message; |
| 113 | + // _buffering = player.isBuffering; |
| 114 | + |
| 115 | + player.addListener(_playerValueChanged); |
| 116 | + |
| 117 | + _currentPosSubs = player.onCurrentPosUpdate.listen((v) { |
| 118 | + setState(() { |
| 119 | + _currentPos = v; |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + _bufferPosSubs = player.onBufferPosUpdate.listen((v) { |
| 124 | + setState(() { |
| 125 | + _bufferPos = v; |
| 126 | + }); |
| 127 | + }); |
| 128 | + } |
| 129 | + |
| 130 | + void _playerValueChanged() { |
| 131 | + FijkValue value = player.value; |
| 132 | + if (value.duration != _duration) { |
| 133 | + setState(() { |
| 134 | + _duration = value.duration; |
| 135 | + }); |
| 136 | + } |
| 137 | + |
| 138 | + bool playing = (value.state == FijkState.started); |
| 139 | + bool prepared = value.prepared; |
| 140 | + String? exception = value.exception.message; |
| 141 | + if (playing != _playing || |
| 142 | + prepared != _prepared || |
| 143 | + exception != _exception) { |
| 144 | + setState(() { |
| 145 | + _playing = playing; |
| 146 | + _prepared = prepared; |
| 147 | + _exception = exception; |
| 148 | + }); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + void _playOrPause() { |
| 153 | + if (_playing == true) { |
| 154 | + player.pause(); |
| 155 | + } else { |
| 156 | + player.start(); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + @override |
| 161 | + void dispose() { |
| 162 | + super.dispose(); |
| 163 | + _hideTimer?.cancel(); |
| 164 | + |
| 165 | + player.removeListener(_playerValueChanged); |
| 166 | + _currentPosSubs?.cancel(); |
| 167 | + _bufferPosSubs?.cancel(); |
| 168 | + } |
| 169 | + |
| 170 | + void _startHideTimer() { |
| 171 | + _hideTimer?.cancel(); |
| 172 | + _hideTimer = Timer(const Duration(seconds: 3), () { |
| 173 | + setState(() { |
| 174 | + _hideStuff = true; |
| 175 | + }); |
| 176 | + }); |
| 177 | + } |
| 178 | + |
| 179 | + void _cancelAndRestartTimer() { |
| 180 | + if (_hideStuff == true) { |
| 181 | + _startHideTimer(); |
| 182 | + } |
| 183 | + setState(() { |
| 184 | + _hideStuff = !_hideStuff; |
| 185 | + }); |
| 186 | + } |
| 187 | + |
| 188 | + Widget _buildVolumeButton() { |
| 189 | + IconData iconData; |
| 190 | + if (_volume <= 0) { |
| 191 | + iconData = Icons.volume_off; |
| 192 | + } else { |
| 193 | + iconData = Icons.volume_up; |
| 194 | + } |
| 195 | + return IconButton( |
| 196 | + icon: Icon(iconData), |
| 197 | + padding: const EdgeInsets.only(left: 10.0, right: 10.0), |
| 198 | + onPressed: () { |
| 199 | + setState(() { |
| 200 | + _volume = _volume > 0 ? 0.0 : 1.0; |
| 201 | + player.setVolume(_volume); |
| 202 | + }); |
| 203 | + }, |
| 204 | + ); |
| 205 | + } |
| 206 | + |
| 207 | + AnimatedOpacity _buildBottomBar(BuildContext context) { |
| 208 | + double duration = _duration.inMilliseconds.toDouble(); |
| 209 | + double currentValue = |
| 210 | + _seekPos > 0 ? _seekPos : _currentPos.inMilliseconds.toDouble(); |
| 211 | + currentValue = min(currentValue, duration); |
| 212 | + currentValue = max(currentValue, 0); |
| 213 | + return AnimatedOpacity( |
| 214 | + opacity: _hideStuff ? 0.0 : 0.8, |
| 215 | + duration: const Duration(milliseconds: 400), |
| 216 | + child: Container( |
| 217 | + height: barHeight, |
| 218 | + color: Theme.of(context).dialogBackgroundColor, |
| 219 | + child: Row( |
| 220 | + children: <Widget>[ |
| 221 | + _buildVolumeButton(), |
| 222 | + Padding( |
| 223 | + padding: const EdgeInsets.only(right: 5.0, left: 5), |
| 224 | + child: Text( |
| 225 | + _duration2String(_currentPos), |
| 226 | + style: const TextStyle(fontSize: 14.0), |
| 227 | + ), |
| 228 | + ), |
| 229 | + |
| 230 | + _duration.inMilliseconds == 0 |
| 231 | + ? const Expanded(child: Center()) |
| 232 | + : Expanded( |
| 233 | + child: Padding( |
| 234 | + padding: const EdgeInsets.only(right: 0, left: 0), |
| 235 | + child: FijkSlider( |
| 236 | + value: currentValue, |
| 237 | + cacheValue: _bufferPos.inMilliseconds.toDouble(), |
| 238 | + min: 0.0, |
| 239 | + max: duration, |
| 240 | + onChanged: (v) { |
| 241 | + _startHideTimer(); |
| 242 | + setState(() { |
| 243 | + _seekPos = v; |
| 244 | + }); |
| 245 | + }, |
| 246 | + onChangeEnd: (v) { |
| 247 | + setState(() { |
| 248 | + player.seekTo(v.toInt()); |
| 249 | + LogD("seek to $v"); |
| 250 | + _currentPos = |
| 251 | + Duration(milliseconds: _seekPos.toInt()); |
| 252 | + _seekPos = -1; |
| 253 | + }); |
| 254 | + }, |
| 255 | + ), |
| 256 | + ), |
| 257 | + ), |
| 258 | + |
| 259 | + // duration / position |
| 260 | + _duration.inMilliseconds == 0 |
| 261 | + ? const Text("LIVE") |
| 262 | + : Padding( |
| 263 | + padding: const EdgeInsets.only(right: 5.0, left: 5), |
| 264 | + child: Text( |
| 265 | + _duration2String(_duration), |
| 266 | + style: const TextStyle( |
| 267 | + fontSize: 14.0, |
| 268 | + ), |
| 269 | + ), |
| 270 | + ), |
| 271 | + |
| 272 | + IconButton( |
| 273 | + icon: Icon( |
| 274 | + widget.player.value.fullScreen |
| 275 | + ? Icons.fullscreen_exit |
| 276 | + : Icons.fullscreen, |
| 277 | + size: 40.w, |
| 278 | + ), |
| 279 | + padding: const EdgeInsets.only(left: 10.0, right: 10.0), |
| 280 | +// color: Colors.transparent, |
| 281 | + onPressed: () { |
| 282 | + widget.player.value.fullScreen |
| 283 | + ? player.exitFullScreen() |
| 284 | + : player.enterFullScreen(); |
| 285 | + }, |
| 286 | + ) |
| 287 | + // |
| 288 | + ], |
| 289 | + ), |
| 290 | + ), |
| 291 | + ); |
| 292 | + } |
| 293 | + |
| 294 | + @override |
| 295 | + Widget build(BuildContext context) { |
| 296 | + Rect rect = player.value.fullScreen |
| 297 | + ? Rect.fromLTWH(0, 0, widget.viewSize.width, widget.viewSize.height) |
| 298 | + : Rect.fromLTRB( |
| 299 | + max(0.0, widget.texturePos.left), |
| 300 | + max(0.0, widget.texturePos.top), |
| 301 | + min(widget.viewSize.width, widget.texturePos.right), |
| 302 | + min(widget.viewSize.height, widget.texturePos.bottom)); |
| 303 | + return Positioned.fromRect( |
| 304 | + rect: rect, |
| 305 | + child: GestureDetector( |
| 306 | + onTap: _cancelAndRestartTimer, |
| 307 | + child: AbsorbPointer( |
| 308 | + absorbing: _hideStuff, |
| 309 | + child: Column( |
| 310 | + children: <Widget>[ |
| 311 | + Container(height: barHeight), |
| 312 | + Expanded( |
| 313 | + child: GestureDetector( |
| 314 | + onTap: () { |
| 315 | + _cancelAndRestartTimer(); |
| 316 | + }, |
| 317 | + child: Container( |
| 318 | + color: Colors.transparent, |
| 319 | + height: double.infinity, |
| 320 | + width: double.infinity, |
| 321 | + child: Center( |
| 322 | + child: _exception != null |
| 323 | + ? Text( |
| 324 | + _exception!, |
| 325 | + style: const TextStyle( |
| 326 | + color: Colors.white, |
| 327 | + fontSize: 25, |
| 328 | + ), |
| 329 | + ) |
| 330 | + : (_prepared || |
| 331 | + player.state == FijkState.initialized) |
| 332 | + ? AnimatedOpacity( |
| 333 | + opacity: _hideStuff ? 0.0 : 0.7, |
| 334 | + duration: const Duration(milliseconds: 400), |
| 335 | + child: IconButton( |
| 336 | + iconSize: 80.w, |
| 337 | + icon: _playing |
| 338 | + ? const ImageIcon( |
| 339 | + AssetImage( |
| 340 | + "assets/images/icon_pause.png"), |
| 341 | + color: Colors.white, |
| 342 | + ) |
| 343 | + : const ImageIcon( |
| 344 | + AssetImage( |
| 345 | + "assets/images/icon_play.png"), |
| 346 | + color: Colors.white), |
| 347 | + // Icon( |
| 348 | + // _playing |
| 349 | + // ? Icons.pause |
| 350 | + // : Icons.play_arrow, |
| 351 | + // color: Colors.white), |
| 352 | + padding: const EdgeInsets.only( |
| 353 | + left: 10.0, right: 10.0), |
| 354 | + onPressed: _playOrPause)) |
| 355 | + : SizedBox( |
| 356 | + width: 70.w, |
| 357 | + height: 70.w, |
| 358 | + child: const CircularProgressIndicator( |
| 359 | + valueColor: AlwaysStoppedAnimation( |
| 360 | + Colors.white)), |
| 361 | + )), |
| 362 | + ), |
| 363 | + ), |
| 364 | + ), |
| 365 | + _buildBottomBar(context), |
| 366 | + ], |
| 367 | + ), |
| 368 | + ), |
| 369 | + ), |
| 370 | + ); |
| 371 | + } |
| 372 | +} |
0 commit comments