129 lines
3.7 KiB
Dart
129 lines
3.7 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:chewie/chewie.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:video_player/video_player.dart';
|
|
|
|
class VideoPlayerScreen extends StatefulWidget {
|
|
final String videoPath;
|
|
|
|
const VideoPlayerScreen({super.key, required this.videoPath});
|
|
|
|
@override
|
|
State<VideoPlayerScreen> createState() => _VideoPlayerScreenState();
|
|
}
|
|
|
|
class _VideoPlayerScreenState extends State<VideoPlayerScreen> {
|
|
late VideoPlayerController _videoPlayerController;
|
|
ChewieController? _chewieController;
|
|
bool _isInitialized = false;
|
|
String? _errorMessage;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_initializePlayer();
|
|
}
|
|
|
|
Future<void> _initializePlayer() async {
|
|
try {
|
|
// Initialize video player controller
|
|
if (widget.videoPath.startsWith('http')) {
|
|
_videoPlayerController = VideoPlayerController.networkUrl(
|
|
Uri.parse(widget.videoPath),
|
|
);
|
|
} else {
|
|
_videoPlayerController = VideoPlayerController.file(
|
|
File(widget.videoPath),
|
|
);
|
|
}
|
|
|
|
await _videoPlayerController.initialize();
|
|
|
|
// Initialize Chewie controller
|
|
_chewieController = ChewieController(
|
|
videoPlayerController: _videoPlayerController,
|
|
autoPlay: true,
|
|
looping: false,
|
|
allowFullScreen: true,
|
|
allowMuting: true,
|
|
showControls: true,
|
|
materialProgressColors: ChewieProgressColors(
|
|
playedColor: Colors.blue,
|
|
handleColor: Colors.white,
|
|
backgroundColor: Colors.white,
|
|
bufferedColor: Colors.grey,
|
|
),
|
|
placeholder: Container(
|
|
color: Colors.black,
|
|
child: const Center(child: CircularProgressIndicator()),
|
|
),
|
|
autoInitialize: true,
|
|
);
|
|
|
|
setState(() {
|
|
_isInitialized = true;
|
|
});
|
|
} catch (e) {
|
|
// Error during video initialization
|
|
}
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_chewieController?.dispose();
|
|
_videoPlayerController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
backgroundColor: Colors.black,
|
|
appBar: AppBar(
|
|
backgroundColor: Colors.black,
|
|
iconTheme: const IconThemeData(color: Colors.white),
|
|
title: const Text(
|
|
'Video Player',
|
|
style: TextStyle(color: Colors.white),
|
|
),
|
|
),
|
|
body: Center(
|
|
child:
|
|
_errorMessage != null
|
|
? Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
Icon(Icons.error_outline, size: 60, color: Colors.red),
|
|
const SizedBox(height: 16),
|
|
Text(
|
|
"Some Error has occured",
|
|
style: const TextStyle(color: Colors.white),
|
|
textAlign: TextAlign.center,
|
|
),
|
|
const SizedBox(height: 16),
|
|
ElevatedButton(
|
|
onPressed: () => Get.back(),
|
|
child: const Text('Go Back'),
|
|
),
|
|
],
|
|
)
|
|
: _isInitialized && _chewieController != null
|
|
? Chewie(controller: _chewieController!)
|
|
: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
const CircularProgressIndicator(),
|
|
const SizedBox(height: 16),
|
|
const Text(
|
|
'Loading video...',
|
|
style: TextStyle(color: Colors.white),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|