-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMediaKitPlayer.dart
More file actions
166 lines (148 loc) · 4.07 KB
/
MediaKitPlayer.dart
File metadata and controls
166 lines (148 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import 'package:flutter/material.dart';
import 'package:media_kit/media_kit.dart';
import 'package:media_kit_video/media_kit_video.dart';
class MediaKitPlayerScreen extends StatefulWidget {
final String videoUrl;
final String? thumbnailUrl;
const MediaKitPlayerScreen({
super.key,
required this.videoUrl,
this.thumbnailUrl,
});
@override
State<MediaKitPlayerScreen> createState() => _MediaKitPlayerScreenState();
}
class _MediaKitPlayerScreenState extends State<MediaKitPlayerScreen> {
late final Player _player;
late final VideoController _controller;
bool _isInitialized = false;
bool _hasError = false;
String? _errorMessage;
@override
void initState() {
super.initState();
initializePlayer();
}
Future<void> initializePlayer() async {
try {
_player = Player();
_controller = VideoController(_player);
// Open the media
await _player.open(Media(widget.videoUrl));
// Listen for errors
_player.stream.error.listen((error) {
setState(() {
_hasError = true;
_errorMessage = error;
});
debugPrint('Video player error: $error');
});
setState(() {
_isInitialized = true;
});
} catch (e) {
setState(() {
_hasError = true;
_errorMessage = e.toString();
});
debugPrint('Error initializing player: $e');
}
}
@override
void dispose() {
_player.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: _buildContent(),
);
}
Widget _buildContent() {
if (_hasError) {
return _buildErrorWidget();
}
if (!_isInitialized) {
return _buildLoadingWidget();
}
return Center(
child: AspectRatio(
aspectRatio: 16 / 9, // Default aspect ratio
child: Video(
controller: _controller,
controls: MaterialVideoControls, // Use material design controls
),
),
);
}
Widget _buildLoadingWidget() {
return const Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
CircularProgressIndicator(color: Colors.white),
SizedBox(height: 16),
Text(
'Loading video...',
style: TextStyle(color: Colors.white),
),
],
),
);
}
Widget _buildErrorWidget() {
return Center(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const Icon(Icons.error_outline, color: Colors.red, size: 60),
const SizedBox(height: 16),
const Text(
'Error Loading Video',
style: TextStyle(
color: Colors.white,
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 8),
Text(
_errorMessage ?? 'Unknown error',
textAlign: TextAlign.center,
style: const TextStyle(color: Colors.white70),
),
const SizedBox(height: 24),
if (widget.thumbnailUrl != null)
Image.network(
widget.thumbnailUrl!,
fit: BoxFit.cover,
width: double.infinity,
height: 240,
errorBuilder: (_, __, ___) => Container(
width: double.infinity,
height: 240,
color: Colors.grey[800],
child: const Icon(Icons.image_not_supported, color: Colors.white54, size: 50),
),
),
],
),
),
);
}
}
//add these packages
// media_kit: ^1.2.0
// media_kit_video: ^1.2.5
// media_kit_libs_video: ^1.0.5