Search code examples
flutterflutter-getxvideo-playerflutter-video-player

How to set limit on Loading when playing video. I'm using Better Player Flutter plugin


When I play a video, it loads the entire video in the first 5 minutes itself. This consumes a lot of my data. I want to load video as it as plays or just for the next five minutes. What functionality should I use for this so that my work becomes easy?

  class VideoView extends GetView<VideoController> {
  VideoController videoController = Get.put(VideoController());
  late BetterPlayerController? _betterPlayerController;
  @override
  Widget build(BuildContext context) {
    Wakelock.enable();
    return Scaffold(
      backgroundColor: Colors.black,
      body: Container(
        height: MediaQuery.of(context).size.height,
        width: MediaQuery.of(context).size.width,
        child: AspectRatio(
          aspectRatio: 16 / 9,
          child: BetterPlayer.network(
            videoController.args[0],
            betterPlayerConfiguration: BetterPlayerConfiguration(
                aspectRatio: 16 / 9,
                controlsConfiguration:
                    BetterPlayerControlsConfiguration(enableFullscreen: false)),
          ),
        ),
      ),
    );
  }

  @override
  void onClose() {
    videoController.dispose();
    Wakelock.disable();
  }
}

Only 2 min video playback and 25 min loaded.


Solution

  • So I got the answer after reading lots of documentation. It was very important to me to reduce the user's data consumption. Most of the users/students were watching videos more than 6 hours in a day. If one hour video consumed more than 400MB-500MB It's not good at all. The user had limited ~1.5 GB Data per day.

    The problem was only with the buffer which was running without limit. `

        import 'package:better_player/better_player.dart';
        import 'package:flutter/material.dart';
        import 'package:get/get.dart';
        import 'package:wakelock/wakelock.dart';
        
        class VideoView extends StatefulWidget {
          @override
          _VideoView createState() => _VideoView();
        }
        
        class _VideoView extends State<VideoView> {
          late BetterPlayerController _betterPlayerController;
        
          @override
          void initState() {
            BetterPlayerConfiguration betterPlayerConfiguration =
                BetterPlayerConfiguration(
                    aspectRatio: 16 / 9,
                    autoDispose: true,
                    autoDetectFullscreenAspectRatio: true,
                    fullScreenByDefault: true,
                    fullScreenAspectRatio: 16 / 9,//AR dual time but it's okay.
                    controlsConfiguration: BetterPlayerControlsConfiguration(
                        enablePip: false,
                        enableFullscreen: true,
                        enableSubtitles: false,
                        loadingColor: Colors.deepOrange,
                        progressBarBufferedColor: Colors.red, //very useful
                        progressBarHandleColor: Colors.blue,
                        progressBarBackgroundColor: Colors.white));
            BetterPlayerDataSource dataSource = BetterPlayerDataSource(
              BetterPlayerDataSourceType.network,
              Get.arguments[2], <== deault quality url
              videoFormat: BetterPlayerVideoFormat.other, //don't forget it if not hsl
              resolutions: {
                "LOW": Get.arguments[3],      // url1
                "MEDIUM": Get.arguments[2],   // url2
                "HD": Get.arguments[1],       // url3
                "Full HD": Get.arguments[0],  // url4 
              },
         // Here is the problem and I resolve it in this way
              bufferingConfiguration: BetterPlayerBufferingConfiguration(
                minBufferMs: 5000,
                maxBufferMs: 131072,
                bufferForPlaybackMs: 2500,
                bufferForPlaybackAfterRebufferMs: 5000,
              ),
           // cacheConfiguration is very useful 
              cacheConfiguration: BetterPlayerCacheConfiguration(
                  useCache: true,
                  maxCacheSize: 10 * 1024 * 1024,
                  maxCacheFileSize: 10 * 1024 * 1024,
                  preCacheSize: 3 * 1024 * 1024),
            );
        
            _betterPlayerController = BetterPlayerController(betterPlayerConfiguration,
                betterPlayerDataSource: dataSource);
            super.initState();
          }
        
          @override
          Widget build(BuildContext context) {
            Wakelock.enable();
            return SafeArea(
              child: Scaffold(
                backgroundColor: Colors.black,
                body: Container(
                  height: MediaQuery.of(context).size.height,
                  width: MediaQuery.of(context).size.width,
                  child: AspectRatio(
                    aspectRatio: 16 / 9, // AR for self satisfaction
                    child: BetterPlayer(
                      controller: _betterPlayerController,
                    ),
                  ),
                ),
              ),
            );
          }
        
          void onClose() {
            _betterPlayerController.dispose(); // necessary
            Wakelock.disable();
          }
        }
    `