Home > Software design >  How to change video on tap with Pod Player in flutter?
How to change video on tap with Pod Player in flutter?

Time:05-01

I am able to load the video initially but when I setState to change the youtube link on tap it doesn't work, don't know why. what is the approach to changing the video on button press? I am using the Pod Player plugin. Just like Load Video Button Functionality in this image

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

class VideoPlayer extends StatefulWidget {
  const VideoPlayer({Key? key}) : super(key: key);

  @override
  State<VideoPlayer> createState() => _VideoPlayerState();
}

class _VideoPlayerState extends State<VideoPlayer> {
  String YTLink = "https://youtu.be/A3ltMaM6noM";

  late final PodPlayerController controller;
  @override
  void initState() {
    controller = PodPlayerController(
      playVideoFrom: PlayVideoFrom.youtube(YTLink),
    )..initialise();
    super.initState();
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
          child: ListView(
        children: [
          PodVideoPlayer(controller: controller),
          OutlinedButton(
              onPressed: () {
                setState(() {
                  YTLink =
                      "https://www.youtube.com/watch?v=Q98aCklzCBE&ab_channel=DWDocumentary";
                });
              },
              child: Text("Play Another Video")),
          OutlinedButton(
              onPressed: () {
                setState(() {
                  YTLink =
                      "https://www.youtube.com/watch?v=AnYsa_c4GxU&ab_channel=FreeDocumentary";
                });
              },
              child: Text("Play Another Video 2")),
        ],
      )),
    );
  }
}

CodePudding user response:

Instead of using setState, use controllers changeVideo method.

Add this to onPressed of Button:

//1

`controller.changeVideo(
                playVideoFrom: PlayVideoFrom.youtube(
                    "https://www.youtube.com/watch?v=Q98aCklzCBE&ab_channel=DWDocumentary"));`

//2

`controller.changeVideo(
                playVideoFrom: PlayVideoFrom.youtube(
                    "https://www.youtube.com/watch?v=AnYsa_c4GxU&ab_channel=FreeDocumentary"));`
  • Related