jakky1 / video_player_win

Flutter video player for Windows, lightweight, using Windows built-in Media Foundation API. Windows implementation of the video_player plugin.
BSD 3-Clause "New" or "Revised" License
35 stars 11 forks source link

Normal in debug mode, the program will crash after selecting a file in release mode #39

Closed daimaxiaofeng closed 3 months ago

daimaxiaofeng commented 3 months ago

This program is very simple, just use file_picker to select a file and play it, but everything works well in debug mode. After building it into release mode, the program will freeze after selecting a file and then exit. There is no error, even flutter run --release --verbose does not output any error.

import 'package:flutter/material.dart';
import 'package:file_picker/file_picker.dart';
import 'package:video_player/video_player.dart';
import 'dart:io';

class VideoPlayerPage extends StatefulWidget {
  const VideoPlayerPage({super.key});

  @override
  VideoPlayerPageState createState() => VideoPlayerPageState();
}

class VideoPlayerPageState extends State<VideoPlayerPage> {
  VideoPlayerController? _controller;
  File? _videoFile;

  @override
  void dispose() {
    super.dispose();
    _controller?.dispose();
  }

  Future<void> _pickVideo() async {
    FilePickerResult? result = await FilePicker.platform.pickFiles(
      type: FileType.video,
    );

    if (result != null) {
      _videoFile = File(result.files.single.path!);
      _controller = VideoPlayerController.file(_videoFile!);

      await _controller!.initialize();

      setState(() {});

      _controller!.play();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Center(
        child: _videoFile == null
            ? Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  ElevatedButton(
                    onPressed: () => _pickVideo(),
                    child: const Text('select video'),
                  )
                ],
              )
            : AspectRatio(
                aspectRatio: _controller!.value.aspectRatio,
                child: VideoPlayer(_controller!),
              ));
  }
}
daimaxiaofeng commented 3 months ago

I tried the same thing on macos with the exact same code and the release version worked fine.

jakky1 commented 3 months ago

I build your code in release mode, then run 5 times select a file, then it play well without any crash / error occurs. So I have no way to reproduce this issue, then of course I cannot fix it...

I run with the following CLI commands:

flutter build windows --release -v
build\windows\x64\runner\Release\video_player_win_example.exe
daimaxiaofeng commented 3 months ago

Are our environments the same? My system is Windows 11. I just tried it again and it's the same.

daimaxiaofeng commented 3 months ago

Here is my complete code. The code at the beginning is src/video_player.dart, and then there are two code files

main.dart

import 'package:flutter/material.dart';
import 'src/home_screen.dart';

class MainApp extends StatelessWidget {
  const MainApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(home: HomeScreen());
  }
}

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(const MainApp());
}

src/home_screen.dart

import 'package:flutter/material.dart';
import 'video_player.dart';

class HomeScreen extends StatelessWidget {
  const HomeScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return const Scaffold(
      body: VideoPlayerPage(),
    );
  }
}
daimaxiaofeng commented 3 months ago

pubspec.yaml

name: demo
description: "A new Flutter project."
publish_to: 'none'
version: 0.1.0

environment:
  sdk: '>=3.4.4 <4.0.0'

dependencies:
  flutter:
    sdk: flutter
  file_picker: ^8.0.6
  video_player: ^2.9.1
  video_player_win: ^2.3.9

dev_dependencies:
  flutter_test:
    sdk: flutter
  flutter_lints: ^3.0.0

flutter:
  uses-material-design: true
jakky1 commented 3 months ago

It still works in my PC.

Windows 11 Flutter 3.22.3 Dart 3.4.4

flutter clean
flutter build windows --release -v
build\windows\x64\runner\Release\video_player_win_example.exe
dependencies:
  file_picker: ^8.0.6
  video_player: ^2.9.1
  video_player_win: ^2.3.9
import 'package:flutter/material.dart';
import 'package:file_picker/file_picker.dart';
import 'package:video_player/video_player.dart';
import 'dart:io';

class MainApp extends StatelessWidget {
  const MainApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(home: HomeScreen());
  }
}

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(const MainApp());
}

class HomeScreen extends StatelessWidget {
  const HomeScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return const Scaffold(
      body: VideoPlayerPage(),
    );
  }
}

class VideoPlayerPage extends StatefulWidget {
  const VideoPlayerPage({super.key});

  @override
  VideoPlayerPageState createState() => VideoPlayerPageState();
}

class VideoPlayerPageState extends State<VideoPlayerPage> {
  VideoPlayerController? _controller;
  File? _videoFile;

  @override
  void dispose() {
    super.dispose();
    _controller?.dispose();
  }

  Future<void> _pickVideo() async {
    FilePickerResult? result = await FilePicker.platform.pickFiles(
      type: FileType.video,
    );

    if (result != null) {
      _videoFile = File(result.files.single.path!);
      _controller = VideoPlayerController.file(_videoFile!);

      await _controller!.initialize();

      setState(() {});

      _controller!.play();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Center(
        child: _videoFile == null
            ? Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  ElevatedButton(
                    onPressed: () => _pickVideo(),
                    child: const Text('select video'),
                  )
                ],
              )
            : AspectRatio(
                aspectRatio: _controller!.value.aspectRatio,
                child: VideoPlayer(_controller!),
              ));
  }
}
daimaxiaofeng commented 3 months ago

I ran your code, and the only difference between our codes seems to be the difference between single file and multiple files, but yours can run, which is strange.

daimaxiaofeng commented 3 months ago

Oh, I solved it. The first time I copied your code directly into a new project, it worked, and then I copied it into my original project, and the problem reappeared. So I tried flutter clean and rebuilt, and it worked. Thank you very much!