I'm using video_player: ^2.10.0 to display a video as a background in my web app, but it is simply not loading. There is this Video init error:
UnimplementedError: init() has not been implemented.
I have tried to mute the video, but still it does not show up.
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';
class WorksSection extends StatefulWidget {
const WorksSection({required this.workKey, super.key});
final GlobalKey workKey;
@override
State<WorksSection> createState() => _WorksSectionState();
}
class _WorksSectionState extends State<WorksSection> {
late VideoPlayerController _controller;
bool _isInitialized = false;
String? _errorMessage;
@override
void initState() {
super.initState();
_initializeVideo();
}
Future<void> _initializeVideo() async {
_controller = VideoPlayerController.asset(
'assets/videos/background_video.mp4',
);
try {
await _controller.initialize();
_controller.setLooping(true);
_controller.setVolume(0);
_controller.play();
setState(() {
_isInitialized = true;
_errorMessage = null;
});
} catch (error) {
setState(() {
_isInitialized = false;
_errorMessage = 'Failed to load video: ${error.toString()}';
});
print('Video init error: $error');
}
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
key: widget.workKey,
child: Stack(
alignment: Alignment.center,
children: [
if (_isInitialized)
AspectRatio(
aspectRatio: _controller.value.aspectRatio,
child: FittedBox(
fit: BoxFit.cover,
child: SizedBox(
width: _controller.value.size.width,
height: _controller.value.size.height,
child: VideoPlayer(_controller),
),
),
)
else if (_errorMessage != null)
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(Icons.error, size: 64, color: Colors.red),
const SizedBox(height: 16),
Text(
_errorMessage!,
style: const TextStyle(color: Colors.red, fontSize: 16),
textAlign: TextAlign.center,
),
const SizedBox(height: 16),
ElevatedButton(
onPressed: _initializeVideo,
child: const Text('Retry'),
),
],
)
else
const Center(child: CircularProgressIndicator()),
],
),
);
}
}
Here I have initialized the video in a function. I added a fall back error message where if there is any error, the fallback will show the video error on the screen. The path of the video is correct, but it still isn't showing up