Skip to content

Commit 9b65ccf

Browse files
committed
feat: Add showSubtitlesPerDefault flag to control subtitles (#648)
Introduced `showSubtitlesPerDefault` in `ChewieController` to allow disabling subtitles by default. Updated controls to respect the flag and aligned behavior across platforms. Added documentation. Closes #648
1 parent c7665a3 commit 9b65ccf

File tree

6 files changed

+45
-12
lines changed

6 files changed

+45
-12
lines changed

README.md

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,17 @@ optionsTranslation: OptionsTranslation(
127127

128128
## Subtitles
129129

130-
> Since version 1.1.0 chewie supports subtitles. Here you can see how to use them.
130+
> Since version 1.1.0, Chewie supports subtitles.
131131
132-
You can provide an `List<Subtitle>` and customize your subtitles with the `subtitleBuilder` function.
132+
Chewie allows you to enhance the video playback experience with text overlays. You can add a `List<Subtitle>` to your `ChewieController` and fully customize their appearance using the `subtitleBuilder` function.
133133

134-
Add subtitles to your `ChewieController` like the following example:
134+
### Showing Subtitles by Default
135+
136+
Chewie provides the `showSubtitlesPerDefault` flag, allowing you to control whether subtitles are displayed automatically when the video starts. By default, this flag is set to `false`.
137+
138+
### Adding Subtitles
139+
140+
Here’s an example of how to add subtitles to your `ChewieController`:
135141

136142
```dart
137143
ChewieController(
@@ -149,9 +155,10 @@ ChewieController(
149155
index: 1,
150156
start: const Duration(seconds: 10),
151157
end: const Duration(seconds: 20),
152-
text: 'Whats up? :)',
158+
text: 'What’s up? :)',
153159
),
154160
]),
161+
showSubtitlesPerDefault: true, // Automatically display subtitles
155162
subtitleBuilder: (context, subtitle) => Container(
156163
padding: const EdgeInsets.all(10.0),
157164
child: Text(
@@ -162,9 +169,16 @@ ChewieController(
162169
);
163170
```
164171

165-
The `index` attribute is for if you want to structure your subtitles in your database and provide your indexes here. `end` and `text` are the key attributes.
172+
### Subtitle Structure
173+
174+
The `Subtitle` model contains the following key attributes:
166175

167-
The Duration defines which part of your video your subtitles should start and end. For example, if your video is 10 minutes long and you want to add a subtitle between: `00:00` and `00:10`'th of a second:
176+
- **`index`**: A unique identifier for the subtitle, useful for database integration.
177+
- **`start`**: The starting point of the subtitle, defined as a `Duration`.
178+
- **`end`**: The ending point of the subtitle, defined as a `Duration`.
179+
- **`text`**: The subtitle text that will be displayed.
180+
181+
For example, if your video is 10 minutes long and you want to add a subtitle that appears between `00:00` and `00:10`, you can define it like this:
168182

169183
```dart
170184
Subtitle(
@@ -175,6 +189,10 @@ Subtitle(
175189
),
176190
```
177191

192+
### Customizing Subtitles
193+
194+
Use the `subtitleBuilder` function to customize how subtitles are rendered, allowing you to modify text styles, add padding, or apply other customizations to your subtitles.
195+
178196
## Example
179197

180198
Please run the app in the [`example/`](https://github.com/brianegan/chewie/tree/master/example) folder to start playing!

example/lib/app/app.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ class _ChewieDemoState extends State<ChewieDemo> {
4141
}
4242

4343
List<String> srcs = [
44-
"https://assets.mixkit.co/videos/preview/mixkit-spinning-around-the-earth-29351-large.mp4",
45-
"https://assets.mixkit.co/videos/preview/mixkit-daytime-city-traffic-aerial-view-56-large.mp4",
46-
"https://assets.mixkit.co/videos/preview/mixkit-a-girl-blowing-a-bubble-gum-at-an-amusement-park-1226-large.mp4"
44+
"https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4",
45+
'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4',
46+
'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerBlazes.mp4',
4747
];
4848

4949
Future<void> initializePlayer() async {
@@ -126,6 +126,7 @@ class _ChewieDemoState extends State<ChewieDemo> {
126126
];
127127
},
128128
subtitle: Subtitles(subtitles),
129+
showSubtitlesPerDefault: true,
129130
subtitleBuilder: (context, dynamic subtitle) => Container(
130131
padding: const EdgeInsets.all(10.0),
131132
child: subtitle is InlineSpan

lib/src/chewie_player.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ class ChewieController extends ChangeNotifier {
288288
this.zoomAndPan = false,
289289
this.maxScale = 2.5,
290290
this.subtitle,
291+
this.showSubtitlesPerDefault = false,
291292
this.subtitleBuilder,
292293
this.customControls,
293294
this.errorBuilder,
@@ -339,6 +340,7 @@ class ChewieController extends ChangeNotifier {
339340
bool? zoomAndPan,
340341
double? maxScale,
341342
Subtitles? subtitle,
343+
bool? showSubtitlesPerDefault,
342344
Widget Function(BuildContext, dynamic)? subtitleBuilder,
343345
Widget? customControls,
344346
WidgetBuilder? bufferingBuilder,
@@ -391,6 +393,8 @@ class ChewieController extends ChangeNotifier {
391393
optionsBuilder: optionsBuilder ?? this.optionsBuilder,
392394
additionalOptions: additionalOptions ?? this.additionalOptions,
393395
showControls: showControls ?? this.showControls,
396+
showSubtitlesPerDefault:
397+
showSubtitlesPerDefault ?? this.showSubtitlesPerDefault,
394398
subtitle: subtitle ?? this.subtitle,
395399
subtitleBuilder: subtitleBuilder ?? this.subtitleBuilder,
396400
customControls: customControls ?? this.customControls,
@@ -454,6 +458,13 @@ class ChewieController extends ChangeNotifier {
454458
/// Add a List of Subtitles here in `Subtitles.subtitle`
455459
Subtitles? subtitle;
456460

461+
/// Determines whether subtitles should be shown by default when the video starts.
462+
///
463+
/// If set to `true`, subtitles will be displayed automatically when the video
464+
/// begins playing. If set to `false`, subtitles will be hidden by default.
465+
///
466+
bool showSubtitlesPerDefault;
467+
457468
/// The controller for the video you want to play
458469
final VideoPlayerController videoPlayerController;
459470

lib/src/cupertino/cupertino_controls.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,8 @@ class _CupertinoControlsState extends State<CupertinoControls>
641641
}
642642

643643
Future<void> _initialize() async {
644-
_subtitleOn = chewieController.subtitle?.isNotEmpty ?? false;
644+
chewieController.showSubtitlesPerDefault &&
645+
(chewieController.subtitle?.isNotEmpty ?? false);
645646
controller.addListener(_updateState);
646647

647648
_updateState();

lib/src/material/material_controls.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,8 @@ class _MaterialControlsState extends State<MaterialControls>
524524
}
525525

526526
Future<void> _initialize() async {
527-
_subtitleOn = chewieController.subtitle?.isNotEmpty ?? false;
527+
_subtitleOn = chewieController.showSubtitlesPerDefault &&
528+
(chewieController.subtitle?.isNotEmpty ?? false);
528529
controller.addListener(_updateState);
529530

530531
_updateState();

lib/src/material/material_desktop_controls.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,8 @@ class _MaterialDesktopControlsState extends State<MaterialDesktopControls>
466466
}
467467

468468
Future<void> _initialize() async {
469-
_subtitleOn = chewieController.subtitle?.isNotEmpty ?? false;
469+
_subtitleOn = chewieController.showSubtitlesPerDefault &&
470+
(chewieController.subtitle?.isNotEmpty ?? false);
470471
controller.addListener(_updateState);
471472

472473
_updateState();

0 commit comments

Comments
 (0)