Video freezes when Admob's ad load Flutter

191

I know its too late. Just posting here to give refernce to others. As I have wasted lots of hours due to it.

I was also facing the same issue when I was using video player with google_mobile_ads ad library

So I just switch to native_admob_flutter ad library on video page. And it is working fine.

Hope this will save some hours of others.

Share:
191
Muhammad Shafique
Author by

Muhammad Shafique

Updated on December 30, 2022

Comments

  • Muhammad Shafique
    Muhammad Shafique over 1 year

    Hy there everyone. I'm new to Flutter. I want to implement Admob ads in my app. Basically my app is about live streaming of News. Whenever the ads load the audio works fine but there is a lag in video. Can anyone help me with this??

    Hy there everyone. I'm new to Flutter. I want to implement Admob ads in my app. Basically my app is about live streaming of News. Whenever the ads load the audio works fine but there is a lag in video. Can anyone help me with this??

    Hy there everyone. I'm new to Flutter. I want to implement Admob ads in my app. Basically my app is about live streaming of News. Whenever the ads load the audio works fine but there is a lag in video. Can anyone help me with this??

    import 'package:better_player/better_player.dart';
    import 'package:cached_network_image/cached_network_image.dart';
    import 'package:flutter/material.dart';
    import 'package:google_fonts/google_fonts.dart';
    import 'package:google_mobile_ads/google_mobile_ads.dart';
    
    class StreamPlayer extends StatefulWidget {
      final String channelURL;
      final String channelName;
      final String channelImage;
      StreamPlayer(
          {@required this.channelURL,
          @required this.channelName,
          @required this.channelImage});
      @override
      _StreamPlayerState createState() => _StreamPlayerState();
    }
    
    class _StreamPlayerState extends State<StreamPlayer> {
      BetterPlayerController _controller;
      BannerAd _bannerAd;
      bool _isBannerAdReady = false;
    
      @override
      void initState() {
        super.initState();
        playerConfig();
        getBanner();
      }
    
      playerConfig() {
        BetterPlayerDataSource betterPlayerDataSource = BetterPlayerDataSource(
            BetterPlayerDataSourceType.network, widget.channelURL,
            liveStream: true);
        _controller = BetterPlayerController(
            BetterPlayerConfiguration(
              aspectRatio: 16 / 9,
              autoDetectFullscreenDeviceOrientation: true,
              autoPlay: true,
            ),
            betterPlayerDataSource: betterPlayerDataSource);
      }
    
      getBanner() {
        _bannerAd = BannerAd(
          adUnitId: 'ca-app-pub-3940256099942544/6300978111', //Ad for Testing
          request: AdRequest(),
          size: AdSize.banner,
          listener: AdListener(
            onAdLoaded: (ad) {
              setState(() {
                _isBannerAdReady = true;
              });
            },
            onAdFailedToLoad: (ad, err) {
              print('Failed to load a banner ad: ${err.message}');
              _isBannerAdReady = false;
              ad.dispose();
            },
          ),
        );
        return _bannerAd.load();
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
              centerTitle: true,
              backgroundColor: Colors.transparent,
              elevation: 0,
              title: Container(
                width: 200,
                height: 45,
                decoration: BoxDecoration(
                    color: Colors.blueAccent.withOpacity(0.3),
                    borderRadius: BorderRadius.all(Radius.circular(30))),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    CachedNetworkImage(
                        imageUrl: widget.channelImage,
                        width: 25,
                        height: 25,
                        fit: BoxFit.contain,
                        placeholder: (context, url) => Icon(
                              Icons.image,
                              size: 25,
                            ),
                        errorWidget: (context, url, error) => Icon(
                              Icons.image,
                              size: 50,
                            )),
                    SizedBox(
                      width: 15,
                    ),
                    Text(widget.channelName,
                        style: GoogleFonts.teko(
                          color: Theme.of(context).primaryColor == Colors.blue
                              ? Colors.black54
                              : Colors.grey[50],
                          fontSize: 22,
                          fontWeight: FontWeight.w600,
                        )),
                    SizedBox(
                      width: 5,
                    ),
                  ],
                ),
              )),
          body: Container(
            child: Column(
              children: [
                Center(
                  child: BetterPlayer(
                    controller: _controller,
                  ),
                ),
                _isBannerAdReady
                    ? Container(
                        height: 100,
                        child: AdWidget(ad: _bannerAd),
                      )
                    : Container(),
              ],
            ),
          ),
        );
      }
    }
    
    
    • Kishan Dhankecha
      Kishan Dhankecha over 2 years
      Did you get any solution?
    • Muhammad Shafique
      Muhammad Shafique over 2 years
      @KishanDhankecha I didn't get any solution. Please tell me if you find any.
  • Muhammad Shafique
    Muhammad Shafique about 2 years
    Thank your for your answer, I will try it sometime.
  • Abdallah A. Odeh
    Abdallah A. Odeh about 2 years
    A weird bug, but works smoothly with the library native_admob_flutter