88 lines
2.1 KiB
Dart
88 lines
2.1 KiB
Dart
|
|
import 'dart:typed_data';
|
|
|
|
import 'package:archive/archive.dart';
|
|
import 'package:flutter/services.dart';
|
|
|
|
class AudioCache {
|
|
|
|
// Filename, bytes
|
|
Map<String, Uint8List> _audioCache = {};
|
|
|
|
List<String> get keys {
|
|
return _audioCache.keys.toList();
|
|
}
|
|
|
|
Uint8List? operator [](String key) {
|
|
// ignore case
|
|
for (var k in _audioCache.keys) {
|
|
if (k.toLowerCase() == key.toLowerCase()) {
|
|
return _audioCache[k];
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
|
|
class AnnouncementCache extends AudioCache {
|
|
|
|
Future<void> loadAnnouncementsFromBytes(Uint8List bytes, List<String> announcements) async {
|
|
|
|
List<String> _announements = [];
|
|
|
|
|
|
// remove any announcements that are already loaded
|
|
for (var announcement in announcements) {
|
|
if (!_audioCache.containsKey(announcement.toLowerCase())) {
|
|
_announements.add(announcement.toLowerCase());
|
|
}
|
|
}
|
|
|
|
if (_announements.length == 0) {
|
|
return;
|
|
}
|
|
|
|
// final bytes = await rootBundle.load(_assetLocation);
|
|
final archive = ZipDecoder().decodeBytes(bytes.buffer.asUint8List());
|
|
|
|
for (final file in archive) {
|
|
|
|
String filename = file.name;
|
|
if (filename.contains("/")) {
|
|
filename = filename.split("/").last;
|
|
}
|
|
|
|
if (_announements.contains(filename.toLowerCase())) {
|
|
_audioCache[filename.toLowerCase()] = file.content;
|
|
print("Loaded announcement: $filename");
|
|
}
|
|
}
|
|
}
|
|
|
|
Future<void> loadAllAnnouncementsFromBytes(Uint8List bytes) async {
|
|
|
|
print("Loading all announcements.");
|
|
|
|
// final bytes = await rootBundle.load(_assetLocation);
|
|
final archive = ZipDecoder().decodeBytes(bytes.buffer.asUint8List());
|
|
|
|
print("Done decoding zip file.");
|
|
print("Number of files: ${archive.length}");
|
|
|
|
for (final file in archive) {
|
|
|
|
if (file.isFile == false) {
|
|
continue;
|
|
}
|
|
|
|
String filename = file.name;
|
|
if (filename.contains("/")) {
|
|
filename = filename.split("/").last;
|
|
}
|
|
|
|
_audioCache[filename.toLowerCase()] = file.content;
|
|
}
|
|
|
|
print("Done loading all announcements.");
|
|
}
|
|
} |