prepareSubtitles method
override
Get the player controller to be used when a media item is loaded up,
Implementation
@override
Future<List<SubtitleItem>> prepareSubtitles({
required AppModel appModel,
required WidgetRef ref,
required MediaItem item,
}) async {
List<SubtitleItem> items = [];
Map<String, dynamic> intentExtra = jsonDecode(item.extra ?? '{}');
List<String>? fileNames;
List<String>? subtitleNames;
if (intentExtra['subs'] != null && intentExtra['subs.name'] != null) {
fileNames ??= List<String>.from(intentExtra['subs']);
subtitleNames ??= List<String>.from(intentExtra['subs.name']);
}
if (fileNames == null && intentExtra['subtitles_location'] != null) {
fileNames = [intentExtra['subtitles_location']];
subtitleNames = ['External'];
}
fileNames ??= [];
subtitleNames ??= [];
for (int i = 0; i < fileNames.length; i++) {
String fileName = fileNames[i];
String subtitleName = subtitleNames[i];
String temporaryDirectoryPath = (await getTemporaryDirectory()).path;
String temporaryFileName =
'jidoujisho-${DateFormat('yyyyMMddTkkmmss').format(DateTime.now())}';
try {
File file = File('$temporaryDirectoryPath/$temporaryFileName.ass');
http.Response request = await http.get(Uri.parse(fileName));
Uint8List bytes = request.bodyBytes;
await file.writeAsBytes(bytes);
SubtitleItem? subtitleItem = await SubtitleUtils.subtitlesFromFile(
file: file,
metadata: subtitleName,
source: fileName,
type: SubtitleItemType.webSubtitle,
);
items.add(subtitleItem);
} catch (e) {
debugPrint('$e');
}
}
String? preferredFilename;
if (intentExtra['subs.enable'] != null) {
List<String> enabledSubtitles =
List<String>.from(intentExtra['subs.enable']);
if (enabledSubtitles.isNotEmpty) {
preferredFilename = enabledSubtitles.first;
}
}
SubtitleItem? preferredItem =
items.firstWhereOrNull((item) => preferredFilename == item.source);
items.remove(preferredItem);
if (preferredItem != null) {
items.insert(0, preferredItem);
}
return items;
}