'When phone is rotated to landscape mode, the bloc builder is building again
I am using better_player package for showing video from an api. The video url is fetched through bloc. Suppose, I am watching the video and reached halfway, when i am trying to switch the video to landscape mode, the Bloc builder is called again and following, the video player is called again which starts the video from the beginning. Is there any fix for this?
Solution 1:[1]
I got a fix for this. You can initialize the video player in the bloc listener as bloc listener doesn't gets called when you rotate the screen.
The code below shows the implementation.
Widget _videoPlayer() {
return BlocConsumer<VideoStreamingBloc, VideoStreamingState>(
bloc: _videoStreamingBloc,
listener: (context, state) {
// TODO: implement listener
if (state is LoadingVideo) {}
if (state is VideoStreamingSuccess) {
BetterPlayerConfiguration betterPlayerConfiguration =
BetterPlayerConfiguration(
aspectRatio: 16 / 9,
fit: BoxFit.fill,
autoPlay: true,
looping: false,
placeholder: _buildVideoPlaceholder(),
showPlaceholderUntilPlay: true,
autoDetectFullscreenDeviceOrientation: false);
BetterPlayerDataSource dataSource = BetterPlayerDataSource(
BetterPlayerDataSourceType.network,
state.videoStreamingResponse.response.toString());
_betterPlayerController =
BetterPlayerController(betterPlayerConfiguration);
_betterPlayerController.setupDataSource(dataSource);
}
},
builder: (context, state) {
if (state is LoadingVideo) {
return AspectRatio(
aspectRatio: 16 / 9,
child: Center(child: ShimmerLoading(
isLoading: true,
child: Container(
decoration: const BoxDecoration(
color: Colors.black,
),
),
),));
}
if (state is Error) {
CustomSnackBar.negativeSnackBar(
context: context,
title: 'Error',
message: state.toString(),
);
}
if (state is VideoStreamingSuccess) {
return state.videoStreamingResponse.response == null
? const AspectRatio(
aspectRatio: 16 / 9,
child: Center(
child: Text(
"No Video Found",
style: TextStyle(fontSize: 18),
),
),
)
: AspectRatio(
aspectRatio: 16 / 9,
child: BetterPlayer(controller: _betterPlayerController));
}
return const SizedBox();
},
);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | viki |