Skip to content

Commit d5d66ea

Browse files
authored
Merge pull request #872 from floodoo/master
feat: Add `showSubtitles` flag to control subtitles (#648)
2 parents 398c1ad + 77798a5 commit d5d66ea

File tree

6 files changed

+43
-12
lines changed

6 files changed

+43
-12
lines changed

README.md

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

130130
## Subtitles
131131

132-
> Since version 1.1.0 chewie supports subtitles. Here you can see how to use them.
132+
> Since version 1.1.0, Chewie supports subtitles.
133133
134-
You can provide an `List<Subtitle>` and customize your subtitles with the `subtitleBuilder` function.
134+
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.
135135

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

138144
```dart
139145
ChewieController(
@@ -151,9 +157,10 @@ ChewieController(
151157
index: 1,
152158
start: const Duration(seconds: 10),
153159
end: const Duration(seconds: 20),
154-
text: 'Whats up? :)',
160+
text: 'What’s up? :)',
155161
),
156162
]),
163+
showSubtitles: true, // Automatically display subtitles
157164
subtitleBuilder: (context, subtitle) => Container(
158165
padding: const EdgeInsets.all(10.0),
159166
child: Text(
@@ -164,9 +171,16 @@ ChewieController(
164171
);
165172
```
166173

167-
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.
174+
### Subtitle Structure
175+
176+
The `Subtitle` model contains the following key attributes:
168177

169-
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:
178+
- **`index`**: A unique identifier for the subtitle, useful for database integration.
179+
- **`start`**: The starting point of the subtitle, defined as a `Duration`.
180+
- **`end`**: The ending point of the subtitle, defined as a `Duration`.
181+
- **`text`**: The subtitle text that will be displayed.
182+
183+
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:
170184

171185
```dart
172186
Subtitle(
@@ -177,6 +191,10 @@ Subtitle(
177191
),
178192
```
179193

194+
### Customizing Subtitles
195+
196+
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.
197+
180198
## Example
181199

182200
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+
showSubtitles: 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: 9 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.showSubtitles = 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? showSubtitles,
342344
Widget Function(BuildContext, dynamic)? subtitleBuilder,
343345
Widget? customControls,
344346
WidgetBuilder? bufferingBuilder,
@@ -391,6 +393,7 @@ class ChewieController extends ChangeNotifier {
391393
optionsBuilder: optionsBuilder ?? this.optionsBuilder,
392394
additionalOptions: additionalOptions ?? this.additionalOptions,
393395
showControls: showControls ?? this.showControls,
396+
showSubtitles: showSubtitles ?? this.showSubtitles,
394397
subtitle: subtitle ?? this.subtitle,
395398
subtitleBuilder: subtitleBuilder ?? this.subtitleBuilder,
396399
customControls: customControls ?? this.customControls,
@@ -454,6 +457,12 @@ class ChewieController extends ChangeNotifier {
454457
/// Add a List of Subtitles here in `Subtitles.subtitle`
455458
Subtitles? subtitle;
456459

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

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.showSubtitles &&
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
@@ -525,7 +525,8 @@ class _MaterialControlsState extends State<MaterialControls>
525525
}
526526

527527
Future<void> _initialize() async {
528-
_subtitleOn = chewieController.subtitle?.isNotEmpty ?? false;
528+
_subtitleOn = chewieController.showSubtitles &&
529+
(chewieController.subtitle?.isNotEmpty ?? false);
529530
controller.addListener(_updateState);
530531

531532
_updateState();

lib/src/material/material_desktop_controls.dart

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

495495
Future<void> _initialize() async {
496-
_subtitleOn = chewieController.subtitle?.isNotEmpty ?? false;
496+
_subtitleOn = chewieController.showSubtitles &&
497+
(chewieController.subtitle?.isNotEmpty ?? false);
497498
controller.addListener(_updateState);
498499

499500
_updateState();

0 commit comments

Comments
 (0)