1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Flutter] How to run a video in flutter windows application (flutter: Error initializing...

Discussão em 'Mobile' iniciado por Stack, Outubro 17, 2024 às 23:02.

  1. Stack

    Stack Membro Participativo

    I am trying to display a video as a screen saver, but it is not running on my flutter application[Windows (desktop)]. and console is throwing error as

    • flutter: Error initializing video: UnimplementedError: init() has not been implemented.

    here's my code :

    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
    );
    }
    }



    Expecting a video to be display run in flutter application

    Continue reading...

Compartilhe esta Página