如何在 flutter windows 应用程序中运行视频(flutter: Error初始化视频: UnimplementedError: init() 尚未实现。)

问题描述 投票:0回答:1

我正在尝试将视频显示为屏幕保护程序,但它没有在我的 flutter 应用程序 [Windows(桌面)] 上运行。控制台抛出错误为

  • flutter:初始化视频时出错:UnimplementedError:init() 尚未实现。

这是我的代码:

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 应用程序中显示运行的视频

flutter dart flutter-dependencies
1个回答
0
投票

发生此错误是因为

video_player
软件包 尚不支持 Windows。你可以在pubdev中看到,这个包只支持:

  • 安卓
  • iOS
  • Mac操作系统
  • 网页

您可以在包标题下方看到兼容性

video_player support

并在描述中

video_player support

© www.soinside.com 2019 - 2024. All rights reserved.