OutdatedGuy / cached_video_player_plus

Original video_player plugin with the superpower of caching embedded in Android and iOS.
https://pub.dev/packages/cached_video_player_plus
BSD 3-Clause "New" or "Revised" License
20 stars 12 forks source link

Only first caption line shows #51

Open Safiyya opened 2 weeks ago

Safiyya commented 2 weeks ago

Hi,

I'm using the following code to initialize my player. On debug, I can see the captions are initialized within the controller but when the video plays, only the first line is shown. If I try to show _controller!.value.caption.start.toString() instead of the caption text, the start is always Duration.zero.

 Future<void> _initializeVideoPlayer() async {
    try {
      setState(() {
        _isError = false;
      });
      if (widget.videoUrlNotifier.value == null) return;

      String srtFileContent = widget.captionFileNotifier?.value != null
          ? widget.captionFileNotifier!.value!.readAsStringSync()
          : "";
      ClosedCaptionFile srtFile = SubRipCaptionFile(srtFileContent);

      final url = Uri.parse(widget.videoUrlNotifier.value!);
      bool isURL = url.isAbsolute;
      if (isURL) {
        _controller = CachedVideoPlayerPlusController.networkUrl(url,
            closedCaptionFile: Future.value(srtFile),
            videoPlayerOptions: VideoPlayerOptions(
                mixWithOthers: widget.backgroundAudioAllowed,
                allowBackgroundPlayback: true));
      } else {
        final File file = File(widget.videoUrlNotifier.value!);

        if (!file.existsSync()) {
          setState(() {
            _isError = true;
          });
        }

        _controller = CachedVideoPlayerPlusController.file(file,
            closedCaptionFile: Future.value(srtFile),
            videoPlayerOptions: VideoPlayerOptions(
                mixWithOthers: widget.backgroundAudioAllowed,
                allowBackgroundPlayback: true));
      }

      await _controller!.initialize();
      await _controller!.setLooping(true);
      await _controller!.play();
      setState(() {});
      Provider.of<MediaPlayerModel>(context, listen: false).videoLoaded();
    } catch (e) {
      setState(() {
        _isError = true;
      });
    }
  }

and the widget looks like

   @override
  Widget build(BuildContext context) {
    return _controller != null && _controller!.value.isInitialized
        ? Stack(
            alignment: Alignment.center,
            children: [
              LayoutBuilder(
                builder: (context, constraints) => Align(
                  alignment: Alignment.center,
                  child: SizedBox(
                    width: constraints.maxWidth,
                    child: AspectRatio(
                      aspectRatio: _controller!.value.aspectRatio,
                      child: Stack(children: [
                        CachedVideoPlayerPlus(_controller!),
                      ]),
                    ),
                  ),
                ),
              ),
              Positioned(
                bottom: 48 * 2,
                child: SizedBox(
                  width: MediaQuery.of(context).size.width * 0.9,
                  child: ClosedCaption(
                    text: _controller!.value.caption.text,
                    textStyle: Theme.of(context)
                        .textTheme
                        .titleSmall!
                        .copyWith(fontWeight: FontWeight.normal),
                  ),
                ),
              ),
            ],
          )
        : Container();
  }
OutdatedGuy commented 2 weeks ago

Hi @Safiyya, can you try this with the plain video_player plugin and tell me if the same issue occurs?

Safiyya commented 1 week ago

@OutdatedGuy It seems like its happening with video_player: ^2.9.2 as well, thanks for the suggestion. I'll check my code and will post here when I find a solution in case it helps someone else!

OutdatedGuy commented 1 week ago

@Safiyya you should try filing an issue in the video_player repo for further assistance and help.