0

So i have a videoController in my childWidget(VideoCard). Parent widget has pageview.builder with childwidget which shows a video,(a video feed list). i need to pause the widget to show interstitial every 4 pages swiped. Right now if i show adview the video in the background is still playing. this is the parent widget code

           StreamBuilder<ApiResponse<List<VideoData>>>(
                stream: feedVideoBloc.videosResponseStream,
                builder: (context, snapshot) {
                  if (snapshot.connectionState == ConnectionState.waiting) {
                    return Center(
                      child: SpinKitThreeBounce(
                        color: Colors.pinkAccent,
                        size: 30,
                      ),
                    );
                  } else if (snapshot.data!.data!.isEmpty) {
                    return Center(
                      child: Text('No Videos'),
                    );
                  } else {
                    return Stack(
                      children: [
                        Container(
                          color: Colors.black,
                          child: PreloadPageView.builder(
                            itemCount: snapshot.data?.data?.length,
                            onPageChanged: (index) {
                              userSwiped++;
                              if (userSwiped == 4) {
                                userSwiped = 0;
                                _showInterstitialAdSwipe();
                              }
                              feedPageIndex = index;
                              print(feedPageIndex);
                              if (index == snapshot.data!.data!.length - 2) {
                                feedVideoBloc.getAllVideos();
                              }
                            },
                            pageSnapping: true,
                            controller: pageViewVideoController,
                            physics: BouncingScrollPhysics(),
                            scrollDirection: Axis.vertical,
                            preloadPagesCount: 1,
                            itemBuilder: (BuildContext context, int index) {
                              if (snapshot.connectionState ==
                                  ConnectionState.waiting) {
                                return CircularProgressIndicator();
                              }
                              if (snapshot.data!.status == Status.LOADING) {}
                              if (snapshot.data!.status == Status.HAS_DATA) {
                                return VideoCard(
                                  followUser: (bool current) {
                                    feedVideoBloc.followUser(
                                        snapshot.data!.data![index].user!.id!,
                                        userModel.token!,
                                        current,
                                        index);
                                  },
                                  likeVideo: (bool current) {
                                    feedVideoBloc.postVideoLike(
                                        snapshot.data!.data![index].id!,
                                        userModel.token!,
                                        current,
                                        index);
                                  },
                                  callback: (String url) {
                                    _showInterstitialAdDownload(index, url);
                                  },
                                  key: Key("unique key $index"),
                                  index: index,
                                  videoData: snapshot.data!.data![index],
                                );
                              }
                              return CircularProgressIndicator();
                            },
                          ),
                        ),
                        Visibility(
                          visible: snapshot.data!.showPaginationLoader &&
                              feedPageIndex == snapshot.data!.data!.length - 1,
                          child: Center(
                              child: SpinKitThreeBounce(
                            color: Colors.pinkAccent,
                            size: 30,
                          )),
                        ),
                      ],
                    );
                  }
                }),

this is the inner videoCard widget

  @override
  void initState() {
    super.initState();
    _videoController = VideoPlayerController.network(widget.videoData.url!);
    _videoController.setLooping(true);
    _initializeVideoPlayerFuture =
        _videoController.initialize().then((value) => setState(() {}));
  }

       AspectRatio(
          aspectRatio: _videoController.value.aspectRatio,
          child: FittedBox(
            fit: BoxFit.contain,
            child: SizedBox(
              width: _videoController.value.size.width,
              height: _videoController.value.size.height,
              child: FutureBuilder<void>(
                  future: _initializeVideoPlayerFuture,
                  builder: (context, snapshot) {
                    if (snapshot.connectionState == ConnectionState.done) {
                      return VisibilityDetector(
                        onVisibilityChanged: (VisibilityInfo info) {
                          debugPrint(
                              "${info.visibleFraction} of my  widget is visible");
                          if (info.visibleFraction == 0) {
                            _videoController.pause();
                          } else {
                            playVideo();
                          }
                        },
                        key: widget.key,
                        child: VideoPlayer(_videoController),
                      );
                    } else
                      return Center(
                        child: SpinKitThreeBounce(
                          color: Colors.pinkAccent,
                          size: 30,
                        ),
                      );
                  }),
            ),
          ),
        ),

1 Answers1

0

You still didn't show where you stated the video controller variable, although I think I know how to solve it,

Just state you variable at the beginning of the page:

Immediately after your imports

Like so:

//all imports
import 'flutter/material.dart';

var _videoController;

This way you are not limiting the use of the controller to only one widget

Jesus Loves You
  • 261
  • 5
  • 17