android 如何通过从TextFormField输入链接来显示视频

wqlqzqxt  于 2022-11-20  发布在  Android
关注(0)|答案(1)|浏览(105)

上下文是TextFormFieldElevatedButton。我输入了视频的URL(通常格式为mp4)并按下按钮。下面将显示视频是从这个URL抓取的。我想我应该使用包video_player。但是,在示例代码中,它需要一个initState上的初始URL(或者onInit,如果使用GetX包的话)。如何使URL动态化并且只在输入URL后显示?

class CyberDropController extends GetxController with StateMixin<List> {
  final dataCyberDrop = DataCyberDrop();
  late VideoPlayerController videoController;
  var url = ''.obs;
  @override
  void onInit() {
    super.onInit();
    playVideo(url.value);
    change(null, status: RxStatus.empty());
  }

  Future<void> playVideo(String url) async {
    videoController = VideoPlayerController.network(url);
    await videoController.initialize();
    await videoController.setLooping(true);
    await videoController.play();
    update();
  }


  void fetch({String? link}) async {
    change(null, status: RxStatus.loading());
    try {
      var data = await dataCyberDrop.scraperCyberDrop(link: link);
      change(data, status: RxStatus.success());
    } catch (e) {
      change(null, status: RxStatus.error(e.toString()));
    }
  }
}

浏览次数:

controller.obx(
                (state) => Column(
                  children: state!
                      .map(
                        (e) => e!.toString().contains('.mp4')
                            ? AspectRatio(
                                aspectRatio: controller
                                    .videoController.value.aspectRatio,
                                child: VideoPlayer(controller.videoController),
                              )
                            : Image.network(e),
                      )
                      .toList(),
sdnqo3pr

sdnqo3pr1#

既然您说您正在使用TextFormField,那么就可以在设置URL之后使用onChanged属性来执行您的方法:

TextFormField(
onChanged: (urlValue) async{ 
 if(/* recommended to set a condition on URL where it shouldn't execute anything*/) {
  return;
  }

  await controller.playVideo(urlValue);
  }
 )

相关问题