You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

214 lines
5.9 KiB

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:video_player/video_player.dart';
import 'package:chewie/chewie.dart';
import 'package:get/get.dart';
/// 视频播放页面
class VideoPlayerPage extends StatefulWidget {
final String videoPath;
final bool isNetwork;
const VideoPlayerPage({
required this.videoPath,
this.isNetwork = false,
super.key,
});
@override
State<VideoPlayerPage> createState() => _VideoPlayerPageState();
}
class _VideoPlayerPageState extends State<VideoPlayerPage> {
late VideoPlayerController _videoPlayerController;
ChewieController? _chewieController;
bool _isLoading = true;
String? _errorMessage;
@override
void initState() {
super.initState();
_initializePlayer();
}
Future<void> _initializePlayer() async {
try {
// 创建视频控制器
if (widget.isNetwork) {
_videoPlayerController = VideoPlayerController.networkUrl(
Uri.parse(widget.videoPath),
);
} else {
_videoPlayerController = VideoPlayerController.file(
File(widget.videoPath),
);
}
// 初始化视频
await _videoPlayerController.initialize();
// 创建 Chewie 控制器
_chewieController = ChewieController(
videoPlayerController: _videoPlayerController,
autoPlay: true,
looping: false,
aspectRatio: _videoPlayerController.value.aspectRatio,
// 显示控制栏
showControls: true,
// 自动隐藏控制栏
autoInitialize: true,
// 错误提示
errorBuilder: (context, errorMessage) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(
Icons.error_outline,
color: Colors.white,
size: 48,
),
const SizedBox(height: 16),
Text(
'播放失败\n$errorMessage',
style: const TextStyle(color: Colors.white),
textAlign: TextAlign.center,
),
],
),
);
},
// 允许全屏
allowFullScreen: true,
// 允许静音
allowMuting: true,
// 显示播放速度选项
allowPlaybackSpeedChanging: true,
// 播放速度选项
playbackSpeeds: const [0.5, 0.75, 1.0, 1.25, 1.5, 2.0],
// 材质进度条颜色
materialProgressColors: ChewieProgressColors(
playedColor: Theme.of(context).primaryColor,
handleColor: Theme.of(context).primaryColor,
backgroundColor: Colors.grey,
bufferedColor: Colors.grey[300]!,
),
);
setState(() {
_isLoading = false;
});
} catch (e) {
setState(() {
_isLoading = false;
_errorMessage = e.toString();
});
print('❌ 视频初始化失败: $e');
}
}
@override
void dispose() {
_chewieController?.dispose();
_videoPlayerController.dispose();
// 恢复系统UI
SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge);
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.black,
body: SafeArea(
child: Stack(
children: [
// 视频播放器
Center(
child: _isLoading
? const CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
)
: _errorMessage != null
? _buildErrorWidget()
: _chewieController != null
? Chewie(controller: _chewieController!)
: const SizedBox.shrink(),
),
// 关闭按钮
Positioned(
top: 16,
left: 16,
child: Material(
color: Colors.black.withOpacity(0.5),
shape: const CircleBorder(),
child: IconButton(
icon: const Icon(
Icons.close,
color: Colors.white,
size: 28,
),
onPressed: () => Get.back(),
),
),
),
],
),
),
);
}
Widget _buildErrorWidget() {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(
Icons.error_outline,
color: Colors.white,
size: 64,
),
const SizedBox(height: 16),
const Text(
'视频加载失败',
style: TextStyle(
color: Colors.white,
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 8),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 32),
child: Text(
_errorMessage ?? '未知错误',
style: const TextStyle(
color: Colors.white70,
fontSize: 14,
),
textAlign: TextAlign.center,
),
),
const SizedBox(height: 24),
ElevatedButton.icon(
onPressed: () {
setState(() {
_isLoading = true;
_errorMessage = null;
});
_initializePlayer();
},
icon: const Icon(Icons.refresh),
label: const Text('重试'),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.white,
foregroundColor: Colors.black,
),
),
],
),
);
}
}