我正在尝试将视频显示为屏幕保护程序,但它没有在我的 flutter 应用程序 [Windows(桌面)] 上运行。控制台抛出错误为
这是我的代码:
import 'dart:async'; // For Timer functionality
import 'dart:io'; // For File access
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: VideoPlayerFromFile(),
);
}
}
class VideoPlayerFromFile extends StatefulWidget {
@override
_VideoPlayerFromFileState createState() => _VideoPlayerFromFileState();
}
class _VideoPlayerFromFileState extends State<VideoPlayerFromFile> {
VideoPlayerController? _controller; // Change to nullable
bool _isBlackScreen = false;
Timer? _blackScreenTimer; // Timer to trigger the black screen every 15 minutes
@override
void initState() {
super.initState();
_initializeVideoPlayer();
_startBlackScreenTimer(); // Start the timer for the 15-minute gap
}
// Function to initialize the video player
Future<void> _initializeVideoPlayer() async {
String filePath = r'C:\videodata\video.mp4'; // Ensure correct path with 'r' to escape backslashes
// Check if the file exists
if (await File(filePath).exists()) {
// Initialize the video player controller with the file path
_controller = VideoPlayerController.file(File(filePath));
// Wait for the video to initialize
await _controller!.initialize().then((_) {
// Once the video is initialized, update the state to start playing
setState(() {
_controller!.play(); // Automatically start the video
});
}).catchError((error) {
print("Error initializing video: $error");
});
} else {
// File does not exist, print error message
print('File does not exist at path: $filePath');
}
}
// Function to start the 15-minute timer for the black screen
void _startBlackScreenTimer() {
_blackScreenTimer = Timer.periodic(Duration(seconds: 15), (timer) {
_showBlackScreen();
});
}
// Function to show the black screen for 7 seconds
void _showBlackScreen() {
setState(() {
_isBlackScreen = true;
});
// Pause the video during the black screen
_controller?.pause();
// After 7 seconds, remove the black screen and resume the video
Future.delayed(Duration(seconds: 7), () {
setState(() {
_isBlackScreen = false;
_controller?.play();
});
});
}
@override
void dispose() {
_controller?.dispose();
_blackScreenTimer?.cancel(); // Cancel the timer when the widget is disposed
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Play Video with Black Screen Interval'),
),
body: Center(
// Show the black screen if triggered, otherwise display the video
child: _isBlackScreen
? Container(
color: Colors.black, // Black screen
)
: _controller != null && _controller!.value.isInitialized
? AspectRatio(
aspectRatio: _controller!.value.aspectRatio,
child: VideoPlayer(_controller!),
)
: Text('Loading video... or File not found'),
),
floatingActionButton: _controller != null && _controller!.value.isInitialized
? FloatingActionButton(
onPressed: () {
setState(() {
// Play or pause the video depending on its current state
if (_controller!.value.isPlaying) {
_controller!.pause();
} else {
_controller!.play();
}
});
},
child: Icon(
_controller!.value.isPlaying ? Icons.pause : Icons.play_arrow,
),
)
: null, // Hide the button if the controller isn't initialized
);
}
}
期望在 flutter 应用程序中显示运行的视频