如何在 flutter 中使用 Pod Player 随时更改视频?

How to change video on tap with Pod Player in flutter?

我最初能够加载视频,但是当我设置状态来更改 youtube link 时,它不起作用,不知道为什么。按下按钮时更改视频的方法是什么? 我正在使用 Pod Player 插件。 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")),
        ],
      )),
    );
  }
}

不使用 setState,而是使用控制器的 changeVideo 方法。

将此添加到按钮的 onPressed:

//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"));`