media-kit / media-kit

A cross-platform video player & audio player for Flutter & Dart.
https://github.com/media-kit/media-kit
MIT License
893 stars 126 forks source link

Wakelock not function #792

Open Anto-2004 opened 3 weeks ago

Anto-2004 commented 3 weeks ago

Hi, I'm using this library, I haven't had any problems, but I noticed that wakelock doesn't work, it's set by default to true in the Video class, but when I watch the video it still goes to stand by, how can I fix this ?

abdelaziz-mahdy commented 3 weeks ago

can you make a reproducible code, and what is the platform you are using? and device? in general give us as much information as possible so we pinpoint the issue and if its fixable from our end, or wakelock themselves

Anto-2004 commented 2 weeks ago

can you make a reproducible code, and what is the platform you are using? and device? in general give us as much information as possible so we pinpoint the issue and if its fixable from our end, or wakelock themselves

This is my code, i have tested on Android, everything works fine but the wakelock doesn't work. As for the device, I use a Samsung s23 for the test.

  ` import 'dart:async';

  import 'package:animeTv/models/episode_page.dart';
  import 'package:animeTv/screens/video-player/fullscreen_episodes_screen.dart';
  import 'package:animeTv/util/constants.dart';
  import 'package:animeTv/widgets/custom-player/base_player.dart';
  import "package:flutter/material.dart";
  import 'package:flutter/services.dart';
  import 'package:flutter_spinkit/flutter_spinkit.dart';
  import 'package:media_kit/media_kit.dart';
  import 'package:media_kit_video/media_kit_video.dart';

  class CustomPlayer extends StatefulWidget {
    final GlobalKey<VideoState> videoKey;
    final VideoController controller;
    final bool fromDownload;
    final EpisodePage? episode;
    final Function callback;
    final int initialPosition;
    final VoidCallback nextEpisode;
    final Function(String episodeLink) startEpisode;
    final List<EpisodeInList>? episodes;

    const CustomPlayer({
      super.key,
      required this.videoKey,
      required this.controller,
      required this.fromDownload,
      required this.episode,
      required this.callback,
      required this.initialPosition,
      required this.nextEpisode,
      required this.startEpisode,
      this.episodes,
    });

    @override
    State<CustomPlayer> createState() => _CustomPlayerState();
  }

  class _CustomPlayerState extends State<CustomPlayer> {
    bool hasLoaded = false;
    bool hasError = false;

    void initController() {
      if (!widget.fromDownload) {
        widget.controller.player.stream.position.listen(
          (Duration position) {
            widget.callback(position);
          },
        );
      }
    }

    void loadVideo(Duration position, String url) async {
      await widget.controller.player.open(Media(url));
      widget.controller.player.stream.duration.listen((event) async {
        if (event.inSeconds > 0) {
          await widget.controller.player.seek(position);
        }
      });
    }

    void _moveSeconds(String moveType) async {
      final currentDuration = widget.controller.player.state.position;
      //aggiungo o sottraggo 10 secondi alla durata iniziale
      final newDuration =
          currentDuration + Duration(seconds: moveType == "forward" ? 10 : -10);

      await widget.controller.player.seek(newDuration);
    }

    void _onClosePlayer() {
      //se sono nei download  chiudo la pagina completamente e torno a quella precedente
      if (widget.fromDownload) {
        Navigator.of(context).popUntil((route) => route.isFirst);
      } else {
        Constants.navigatorKey.currentState?.pop();
      }

      SystemChrome.setPreferredOrientations([
        DeviceOrientation.portraitUp,
        DeviceOrientation.portraitDown,
      ]);

      SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: [SystemUiOverlay.top, SystemUiOverlay.bottom]);
    }

    void _openEpisodes() {
      widget.controller.player.pause();
      Constants.navigatorKey.currentState!
          .push(MaterialPageRoute(builder: (context) {
        return FullscreenEpisodesScreen(
          allEpisodes: widget.episodes!,
          startEpisode: (String episodeLink) {
            widget.startEpisode(episodeLink);
          },
          onClose: () {
            widget.controller.player.play();
          },
        );
      }));
    }

    Future<void> initPlayer({
      required Duration position,
    }) async {
      loadVideo(position, widget.episode!.videoUrl);
    }

    initAll() async {
      initController();
      initPlayer(position: Duration(seconds: widget.initialPosition));

      setState(() {
        hasLoaded = true;
      });
    }

    @override
    void initState() {
      initAll();
      super.initState();
    }

    @override
    Widget build(BuildContext context) {
      return BasePlayer(
        controller: widget.controller,
        nextEpisode: widget.nextEpisode,
        moveSeconds: (String type) {
          _moveSeconds(type);
        },
        onClosePlayer: () {
          _onClosePlayer();
        },
        openEpisodes: widget.episodes != null ? _openEpisodes : null,
        fromDownload: widget.fromDownload,
        body: hasError
            ? Container(
                alignment: Alignment.center,
                color: Theme.of(context).colorScheme.background,
                child: const Text(
                  "Sembra che il video sia ancora in fase di elaborazione,\n riprova più tardi",
                  textAlign: TextAlign.center,
                ),
              )
            : hasLoaded
                ? Video(
                    key: widget.videoKey,
                    controller: widget.controller,
                    controls: MaterialVideoControls,
                    onEnterFullscreen: () async {
                      await defaultEnterNativeFullscreen();
                    },
                    onExitFullscreen: () async {
                      _onClosePlayer();
                    },
                  )
                : Flexible(
                    child: Center(
                      child: SpinKitThreeInOut(
                        color: Theme.of(context).colorScheme.primary,
                        size: 30,
                      ),
                    ),
                  ),
      );
    }
  }`
abdelaziz-mahdy commented 2 weeks ago

Please give a test code with the problem that I can run,

Better question what are you testing on and does the problem happen on the example app? If not then it's a problem on your side check that you don't disable wake lock by mistake