-
Notifications
You must be signed in to change notification settings - Fork 19
Subtitles on Internet
You have two ways to use subtitles on the internet, firstly using SubtitleController
and it's the
easly way, secondly by using SubtitleProvider
and SubtitleParser
, this is both of ways:
Firstly provide the link of subtitle (should be an object of Uri),
after that you can use SubtitleProvider.fromNetwork(url)
or NetworkSubtitle(url)
:
var url = Uri.parse(
'https://brenopolanski.github.io/html5-video-webvtt-example/MIB2-subtitles-pt-BR.vtt');
var controller = SubtitleController(
provider: SubtitleProvider.fromNetwork(url),
);
Or
var url = Uri.parse(
'https://brenopolanski.github.io/html5-video-webvtt-example/MIB2-subtitles-pt-BR.vtt');
var controller = SubtitleController(
provider: NetworkSubtitle(url),
);
Like the last one, provide the link and provider, after that create a new object of SubtitleObject
and await
it to preparing
the subtitle data, in the last you can use SubtitleParser
to parsing the subtitles:
var url = Uri.parse(
'https://brenopolanski.github.io/html5-video-webvtt-example/MIB2-subtitles-pt-BR.vtt');
SubtitleProvider provider = NetworkSubtitle(url);
SubtitleObject object = await provider.getSubtitle();
SubtitleParser parser = SubtitleParser(object);
After creating the controller you have to initialize it by call controller.initial()
, it's async
function, so you have to await
it.
await controller.initial();
When you intialize it, you can use this methods to dealing with subtitles using SubtitleController, see the SubtitleController Documentation for more. Check the API Reference for more.
import 'package:subtitle/subtitle.dart';
void main(List<String> args) async {
//! By using controller - it's easly way
var url = Uri.parse(
'https://brenopolanski.github.io/html5-video-webvtt-example/MIB2-subtitles-pt-BR.vtt');
var controller = SubtitleController(
provider: SubtitleProvider.fromNetwork(
url,
type: SubtitleType.vtt,
));
await controller.initial();
printResult(controller.subtitles);
}
void printResult(List<Subtitle> subtitles) {
subtitles.sort((s1, s2) => s1.compareTo(s2));
for (var result in subtitles) {
print(
'(${result.index}) Start: ${result.start}, end: ${result.end} [${result.data}]');
}
}