jhomlala / betterplayer

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

[BUG] Video starts from 00:00 on device orientation change #604

Closed sha3rawi33 closed 3 years ago

sha3rawi33 commented 3 years ago

History check I checked previous error history and nearly found nothing helpful.

Describe the bug

To Reproduce Steps to reproduce the behavior:

  1. Play video.
  2. Use the full-screen button or rotate the screen.
  3. [ERROR] video will start from the beginning.

*Example code I'm using the demo code of the "resolutions" page HERE

EXCEPT I'm passing the links of the videos on navigation like:

Navigator.of(context).pushNamed(PlayerScreen.routeName, arguments: NavigatorArguments(videoId /*vimeo video ID*/, videoLinks /*Map variable with resolutions links like {"360p":"url.mp4"}*/, lesson.title/*Name of video lesson*/, myLoadedCourse.instructor/*video author*/));

Player Screen Code:

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

// Class for accessing navigator's passed data
class NavigatorArguments {
  final String videoId; // Vimeo video ID to be used as cache key
  final Map videoLinks; // a Map containing video links like: {"1080p":"video_url.mp4"}
  final String title; // video title to be shown on the notification player
  final String author; // video author to be shown on the notification player

  //initialize
  NavigatorArguments(
      this.videoId,
      this.videoLinks,
      this.title,
      this.author,
      );
}

class PlayerScreen extends StatefulWidget {
  static const routeName = '/player';

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

class _PlayerScreenState extends State<PlayerScreen> {
  NavigatorArguments args; //this will carry all passed data

  BetterPlayerController _betterPlayerController;
  GlobalKey _betterPlayerKey = GlobalKey();

  @override
  void initState() {
    BetterPlayerConfiguration betterPlayerConfiguration = BetterPlayerConfiguration(
      aspectRatio: 16 / 9,
      fit: BoxFit.contain,
      autoPlay: true,
      allowedScreenSleep: false,
      looping: false,
      controlsConfiguration: BetterPlayerControlsConfiguration(
        enableSubtitles: false,
        enableAudioTracks: false,
        overflowMenuIcon: Icons.settings,
        loadingColor: Colors.yellowAccent,
        iconsColor: Colors.yellowAccent,
        progressBarBufferedColor: Colors.yellowAccent.shade200,
        enableMute: false,
        backwardSkipTimeInMilliseconds: 10000,
        forwardSkipTimeInMilliseconds: 10000,
        enablePip: true,
      ),
    );

    _betterPlayerController = BetterPlayerController(betterPlayerConfiguration);
    _betterPlayerController.setBetterPlayerGlobalKey(_betterPlayerKey);

    super.initState();
  }

  @override
  Widget build(BuildContext context) {

    //getting the arguments
    args = ModalRoute.of(context).settings.arguments;
    BetterPlayerDataSource dataSource = BetterPlayerDataSource(
      BetterPlayerDataSourceType.network,
      args.videoLinks.values.first, // the first Map item [360p video]
      resolutions: args.videoLinks, // the full video Map items {"360p":"video_url.mp4","480p":"480.mp4"}
      cacheConfiguration: BetterPlayerCacheConfiguration(
        useCache: true,
        preCacheSize: 500 * 1024 * 1024,
        maxCacheSize: 500 * 1024 * 1024,
        maxCacheFileSize: 500 * 1024 * 1024,

        ///Android only option to use cached video between app sessions
        key: args.videoId, // Passed Vimeo Video ID
      ),
      notificationConfiguration: BetterPlayerNotificationConfiguration(
        showNotification: true,
        title: args.title, // Passed video title
        author: args.author, // passed video author
        imageUrl: "https://i.imgur.com/2pUfRgG.png", // notification image
        activityName: "MainActivity",
      ),
    );
    _betterPlayerController.setupDataSource(dataSource);
    _betterPlayerController.isPictureInPictureSupported();
    return Scaffold(
      appBar: AppBar(
        title: Text(args.title), //video title
        centerTitle: true,
      ),
      body: SingleChildScrollView(
        child: Column(mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, children: [
          SizedBox(
            height: MediaQuery.of(context).size.height / 4,
          ),
          AspectRatio(
            aspectRatio: 16 / 9,
            child: Stack(
              children: [
                BetterPlayer(
                  controller: _betterPlayerController,
                  key: _betterPlayerKey,
                ),
                Transform.rotate(
                  angle: -0.45,
                  child: IgnorePointer(
                    child: Opacity(
                      opacity: 0.15,
                      child: Container(
                        child: FittedBox(
                            child: Text(
                          // user.email,
                              "user_email@example.com", //watermark
                          style: TextStyle(color: Colors.black, fontSize: 35, fontWeight: FontWeight.w400),
                        )),
                        width: MediaQuery.of(context).size.width,
                        alignment: Alignment.center,
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
        ]),
      ),
    );
  }
}

Screenshots If applicable, add screenshots to help explain your problem.

https://user-images.githubusercontent.com/36058880/125723175-5e8b4134-e84c-4466-89e6-746a3a972eef.mp4

Expected behavior The video should rotate between portrait and landscape without starting from the beginning

Flutter doctor

Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 2.2.2, on Microsoft Windows [Version 10.0.19042.1083], locale en-US)
[√] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
[√] Chrome - develop for the web
[√] Android Studio
[√] Connected device (3 available)

• No issues found!

Better Player version

Smartphone: tested on more than one device but the problem exists

sha3rawi33 commented 3 years ago

I could fix it by myself. thanks :)

Sumit258000 commented 4 months ago

@sha3rawi33 Can you please explain or share code to fix this. In my case player got pause and audio still gets coming in background. Something like this. issue