xCMD_XMedia_OpenVideo method

Future xCMD_XMedia_OpenVideo(
  1. XMediaItem xMedia, {
  2. String? pathPreviewVideo,
  3. void setState(
    1. void ()
    )?,
})

Comando per aprire un video degli XMedia

Implementation

Future xCMD_XMedia_OpenVideo(XMediaItem xMedia, {String? pathPreviewVideo, void Function(void Function())? setState}) async {
  isBusy.value = false;
  setState!(() {
    xWaiterMessage = "Caricamento del Video, Attendi un attimo..";
    xMedia.isSel = true;
  });
  if (xMedia.payload == null) {
    await xOnGetXMedia(xMedia).then((x) {
      if (x != null) xMedia.payload = x.payload;
    });
  }
  xMedia.isSel = false;
  bool _isPlaying = false;

  Future<void> _playVideo() async {
    await videoPlayerController!.play();
    setState(() {});
  }

  Future<void> _pauseVideo() async {
    await videoPlayerController!.pause();
    setState(() {});
  }

  Future<void> _stopVideo() async {
    await videoPlayerController!.pause();
    await videoPlayerController!.seekTo(Duration.zero);
    setState(() {});
  }

  bool isPlaying;
  File x = await (xMedia.localURI != "" || pathPreviewVideo != null) ? File.fromUri(Uri.parse(pathPreviewVideo ?? xMedia.localURI!)) : File("${XLibStorage.filePath}/XMEDIAS/${xMedia.id}_.mp4");
  await x.create(recursive: true);
  await x.writeAsBytes(xMedia.payload!).then((value) async {
    videoPlayerController = await VideoPlayerController.file(value, videoPlayerOptions: VideoPlayerOptions(allowBackgroundPlayback: true));

    await videoPlayerController!
      ..addListener(() {
        isPlaying = videoPlayerController!.value.isPlaying;
        if (isPlaying != _isPlaying) setState(() => _isPlaying = isPlaying);
      })
      ..initialize().then((_) => setState(() {}));
    await videoPlayerController!.play();

    return await showDialog(
        context: context,
        builder: (context) {
          return StatefulBuilder(builder: (context, setState) {
            var isPlay = videoPlayerController!.value.isPlaying;
            return XAlertDialog(
                content_insetPadding: EdgeInsets.all(0),
                title_TextAlign: TextAlign.center,
                actionsBTNarea_Padding: EdgeInsets.all(0),
                title_Text: "Video",
                actionsBTNarea_Child: Row(mainAxisAlignment: MainAxisAlignment.center, children: [
                  isPlay
                      ? Expanded(
                          child: XBtnbase(
                          label: "Restart",
                          onPressed: () {
                            setState(() {
                              _stopVideo();
                              isPlay = false;
                            });
                          },
                          label_Style: XStyles.xStyTextForLabel(textColor: Colors.yellow),
                        ))
                      : Container(),
                  Expanded(
                      child: XBtnbase(
                    label: isPlay ? "Pausa" : "Play",
                    onPressed: () {
                      setState(() {
                        if (isPlay) {
                          _pauseVideo();
                          isPlay = false;
                        } else {
                          _playVideo();
                          isPlay = true;
                        }
                      });
                    },
                    label_Style: XStyles.xStyTextForLabel(textColor: Colors.green),
                  )),
                  Expanded(
                      child: XBtnbase(
                    label: "Chiudi",
                    onPressed: () {
                      videoPlayerController!.dispose();
                      Navigator.pop(context);
                    },
                    label_Style: XStyles.xStyTextForLabel(textColor: Colors.red),
                  ))
                ]),
                child: Stack(children: [
                  InkWell(
                      child: Container(width: MediaQuery.of(context).size.width, height: MediaQuery.of(context).size.height, child: VideoPlayer(videoPlayerController!)),
                      onTap: () {
                        setState(() {
                          if (isPlay) {
                            _pauseVideo();
                            isPlay = false;
                          } else {
                            _playVideo();
                            isPlay = true;
                          }
                        });
                      })
                ]));
          });
        }).whenComplete(() {
      setState(() {
        xWaiterMessage = "";
        isBusy.value = true;
      });
    });
  });
}