diff --git a/lib/pages/live.dart b/lib/pages/live.dart index bdcc87a..477b78e 100644 --- a/lib/pages/live.dart +++ b/lib/pages/live.dart @@ -139,17 +139,13 @@ class _LivePage extends State Widget build(BuildContext context) { final mq = MediaQuery.of(context); return PopScope( - canPop: false, + canPop: !_streaming, onPopInvokedWithResult: (didPop, result) async { - if (_streaming) { + if (_streaming && !didPop) { final go = await showExitStreamDialog(context); - if (context.mounted) { - if (go == true) { - context.go("/"); - } + if (context.mounted && go == true) { + context.go("/"); } - } else { - context.go("/"); } }, child: ValueListenableBuilder( diff --git a/lib/pages/stream.dart b/lib/pages/stream.dart index 5eb18ec..74f9f7b 100644 --- a/lib/pages/stream.dart +++ b/lib/pages/stream.dart @@ -57,7 +57,7 @@ class StreamPage extends StatefulWidget { class _StreamPage extends State with RouteAware { bool _offScreen = false; - StreamEvent? _stream; + final GlobalKey _playerKey = GlobalKey(); bool isWidgetVisible(BuildContext context) { final router = GoRouter.of(context); @@ -132,11 +132,11 @@ class _StreamPage extends State with RouteAware { final streamWidget = _buildPlayer(ctx, stream); return ValueListenableBuilder( valueListenable: mainPlayer.state, - builder: (context, state, player) { - if (state?.isPortrait == true) { - return _buildPortraitStream(context, stream, player!); + builder: (context, playerState, child) { + if (playerState?.isPortrait == true) { + return _buildPortraitStream(context, stream, child!); } - return _buildLandscapeStream(context, stream, player!); + return _buildLandscapeStream(context, stream, child!); }, child: streamWidget, ); @@ -147,6 +147,7 @@ class _StreamPage extends State with RouteAware { Widget _buildPlayer(BuildContext context, StreamEvent stream) { return (stream.info.stream != null && !_offScreen) ? MainVideoPlayerWidget( + key: _playerKey, url: stream.info.stream!, placeholder: stream.info.image, isLive: true, @@ -170,7 +171,6 @@ class _StreamPage extends State with RouteAware { return Stack( children: [ - child, Positioned(child: child), Positioned( child: Container( diff --git a/lib/player.dart b/lib/player.dart index 15ee90c..dafb400 100644 --- a/lib/player.dart +++ b/lib/player.dart @@ -60,7 +60,7 @@ class MainPlayer extends BaseAudioHandler { Future dispose() async { await super.stop(); await _controller?.dispose(); - _chewieController!.dispose(); + _chewieController?.dispose(); _controller = null; _chewieController = null; state.value = null; @@ -126,6 +126,7 @@ class MainPlayer extends BaseAudioHandler { : null, ); + // insert media item mediaItem.add( MediaItem( @@ -139,6 +140,8 @@ class MainPlayer extends BaseAudioHandler { : null, ), ); + // Update player state immediately after initialization + updatePlayerState(); _url = url; } catch (e) { if (e is PlatformException && e.code == "VideoError") { @@ -179,10 +182,13 @@ class MainPlayer extends BaseAudioHandler { }, ), ); - state.value = PlayerState( - width: _controller!.value.size.width.floor(), - height: _controller!.value.size.height.floor(), - isPlaying: isPlaying, - ); + + if (_controller?.value.isInitialized == true && _controller!.value.size != Size.zero) { + state.value = PlayerState( + width: _controller!.value.size.width.floor(), + height: _controller!.value.size.height.floor(), + isPlaying: isPlaying, + ); + } } }