Turn on and off the subtitle in flutter chewie player

Viewed 376

I am doing a flutter app to play video using flutter chewie player. I have given subtitle wrapper to display subtitle from a url in it. but i want to create a button in video playing screen to turn on and off the subtitle.Can anyone help on how to turn on and off the subtitle in subtitle wrapper. my subtitle wrapper code is as below:

 SubTitleWrapper(
 videoPlayerController: _videoPlayerController,
 subtitleController: SubtitleController(
subtitleUrl:subtitle url,
showSubtitles: true),
videoChild: Chewie(controller: chewieController, ), ),

it will be help full if there is any example for the answer.

1 Answers

You can pass an extra widget as data to the chewie controller's additionalOptions parameter and set an action, as per your requirement toggle subtitle, based on the tap of the widget.

Steps:

First, create a subtitle controller so that we can toggle subtitle on user's click:

final _subtitleController = SubtitleController(
    subtitleUrl:subtitle url,
    showSubtitles: true);

Now pass the subtitle widget as additional data to the chewie controller:

final chewieController = ChewieController(
  additionalOptions: (context) {
  return <OptionItem>[
    OptionItem(
      onTap: () => _subtitleController.showSubtitles = !_subtitleController.showSubtitles,
      iconData: Icons.subtitles,
      title: 'Toggle Subtitle',
    )
  ];
},
 // ...other parameters
);

Now, make a change to your subtitle wrapper as:

SubTitleWrapper(
   subtitleController: _subtitleController
   ... other parameters
)
Related