jhomlala / betterplayer

Better video player for Flutter, with multiple configuration options. Solving typical use cases!
Apache License 2.0
930 stars 1.03k forks source link

[BUG] addEventsListener not working #485

Closed MinseokKang003 closed 3 years ago

MinseokKang003 commented 3 years ago

Hi, thank you for the better player package. I want help because BetterPlayerController.addEventsListener does not listen to play or pause events. Sorry if I'm violating the report guidelines, I've never done it before.

When I try out this code,

import 'package:better_player/better_player.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class OtherPage extends StatefulWidget {
  @override
  _OtherPageState createState() => _OtherPageState();
}

class _OtherPageState extends State<OtherPage> {
  BetterPlayerController _betterPlayerController;

  @override
  void initState() {
    var dataSource = BetterPlayerDataSource(
      BetterPlayerDataSourceType.network,
      "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerBlazes.mp4",
      subtitles: BetterPlayerSubtitlesSource.single(
        type: BetterPlayerSubtitlesSourceType.network,
        url: "https://dl.dropboxusercontent.com/s/71nzjo2ux3evxqk/example_subtitles.srt",
      ),
    );

    _betterPlayerController = BetterPlayerController(
      BetterPlayerConfiguration(
        controlsConfiguration: BetterPlayerControlsConfiguration(enableProgressText: true),
      ),
    );
    _betterPlayerController.addEventsListener((event) {
      print("Better player event: ${event.betterPlayerEventType}");
    });
    _betterPlayerController.setupDataSource(dataSource);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Other page"),
      ),
      body: AspectRatio(
        aspectRatio: 16 / 9,
        child: BetterPlayer.network(
          "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerBlazes.mp4",
          betterPlayerConfiguration: BetterPlayerConfiguration(
            aspectRatio: 16 / 9,
            fullScreenAspectRatio: 9 / 16,
            allowedScreenSleep: false,
            deviceOrientationsOnFullScreen: [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown],
            routePageBuilder: (BuildContext context, Animation<double> animation, Animation<double> secondAnimation, provider) {
              return BetterPlayerCustomRoute(animation: animation, provider: provider);
            },
            controlsConfiguration: BetterPlayerControlsConfiguration(
              enableSkips: false,
              overflowMenuCustomItems: [
                BetterPlayerOverflowMenuItem(
                  Icons.account_circle_rounded,
                  "Custom element",
                  () {
                    print("Click!");
                  },
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  @override
  void dispose() {
    _betterPlayerController.dispose();
    super.dispose();
  }
}

class BetterPlayerCustomRoute extends StatefulWidget {
  final Animation<double> animation;
  final BetterPlayerControllerProvider provider;

  const BetterPlayerCustomRoute({Key key, this.animation, this.provider}) : super(key: key);

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

class _BetterPlayerCustomRouteState extends State<BetterPlayerCustomRoute> {
  bool _controlsVisible = true;

  @override
  void initState() {
    widget.provider.controller.addEventsListener((event) {
      if (event.betterPlayerEventType == BetterPlayerEventType.controlsVisible) {
        setState(() {
          _controlsVisible = true;
        });
      } else if (event.betterPlayerEventType == BetterPlayerEventType.controlsHidden) {
        setState(() {
          _controlsVisible = false;
        });
      }
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
      animation: widget.animation,
      builder: (BuildContext context, Widget child) {
        return Scaffold(
          backgroundColor: Colors.black,
          resizeToAvoidBottomInset: false,
          body: Stack(children: [
            Container(
              alignment: Alignment.center,
              child: widget.provider,
            ),
            _getCenteredWidget(),
          ]),
        );
      },
    );
  }

  Widget _getCenteredWidget() {
    return Opacity(
      opacity: _controlsVisible ? 1.0 : 0.0,
      child: Center(
        child: Text(
          "EXAMPLE WIDGET",
          style: TextStyle(fontSize: 20, color: Colors.red),
        ),
      ),
    );
  }
}

I only get events related with initialization, but not the play, pause, controlsVisible, controlsHidden, etc. events. And also the centeredWidget does not get hidden.

I/flutter (15172): Better player event: BetterPlayerEventType.setupDataSource
I/flutter (15172): Better player event: BetterPlayerEventType.changedSubtitles
I/flutter (15172): Better player event: BetterPlayerEventType.progress
I/flutter (15172): Better player event: BetterPlayerEventType.progress
I/flutter (15172): Better player event: BetterPlayerEventType.initialized
I/flutter (15172): Better player event: BetterPlayerEventType.progress
I/flutter (15172): Better player event: BetterPlayerEventType.changedTrack

Flutter doctor

[√] Flutter (Channel stable, 2.0.6, on Microsoft Windows [Version 10.0.18363.1500], locale ko-KR)
    • Flutter version 2.0.6 at D:\app\flutter
    • Framework revision 1d9032c7e1 (2 weeks ago), 2021-04-29 17:37:58 -0700
    • Engine revision 05e680e202
    • Dart version 2.12.3

[√] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
    • Android SDK at D:\app\android-sdk
    • Platform android-30, build-tools 30.0.2
    • ANDROID_SDK_ROOT = D:\app\android-sdk
    • Java binary at: D:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Android Studio (version 4.1.0)
    • Android Studio at D:\Program Files\Android\Android Studio
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)

[√] Connected device (3 available)
    • SM G965N (mobile) • 211171a60b057ece • android-arm64  • Android 10 (API 29)
    • Chrome (web)      • chrome           • web-javascript • Google Chrome 90.0.4430.212
    • Edge (web)        • edge             • web-javascript • Microsoft Edge 90.0.818.56

• No issues found!

Better Player version

Smartphone (please complete the following information):

MinseokKang003 commented 3 years ago

Found out I didn't apply the betterplayercontroller in initState to the player in build. What a stupid mistake.