nice
This commit is contained in:
@@ -7,6 +7,8 @@ import 'package:bus_infotainment/backend/live_information.dart';
|
||||
import 'package:bus_infotainment/tfl_datasets.dart';
|
||||
import 'package:bus_infotainment/utils/audio%20wrapper.dart';
|
||||
import 'package:bus_infotainment/utils/delegates.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
import 'info_module.dart';
|
||||
|
||||
@@ -19,6 +21,28 @@ class AnnouncementModule extends InfoModule {
|
||||
refreshTimer();
|
||||
}
|
||||
|
||||
// Files
|
||||
String _bundleLocation = "assets/ibus_recordings.zip";
|
||||
Uint8List? _bundleBytes;
|
||||
void setBundleBytes(Uint8List bytes) {
|
||||
_bundleBytes = bytes;
|
||||
}
|
||||
Future<Uint8List> getBundleBytes() async {
|
||||
|
||||
if (_bundleBytes != null) {
|
||||
return _bundleBytes!;
|
||||
} else {
|
||||
|
||||
if (kIsWeb) {
|
||||
throw Exception("Cannot load bundle bytes on web");
|
||||
}
|
||||
|
||||
final bytes = await rootBundle.load(_bundleLocation);
|
||||
return bytes.buffer.asUint8List();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Queue
|
||||
List<AnnouncementQueueEntry> queue = [];
|
||||
AnnouncementQueueEntry? currentAnnouncement;
|
||||
@@ -63,23 +87,34 @@ class AnnouncementModule extends InfoModule {
|
||||
onAnnouncement.trigger(currentAnnouncement!);
|
||||
|
||||
if (currentAnnouncement!.audioSources.isNotEmpty) {
|
||||
try {
|
||||
|
||||
audioPlayer.loadSource(AudioWrapperAssetSource("assets/audio/5-seconds-of-silence.mp3"));
|
||||
audioPlayer.play();
|
||||
await Future.delayed(const Duration(milliseconds: 300));
|
||||
audioPlayer.stop();
|
||||
|
||||
// try {
|
||||
for (AudioWrapperSource source in currentAnnouncement!.audioSources) {
|
||||
try {
|
||||
await audioPlayer.loadSource(source);
|
||||
|
||||
await audioPlayer.loadSource(source);
|
||||
|
||||
Duration? duration = await audioPlayer.play();
|
||||
await Future.delayed(duration!);
|
||||
if (currentAnnouncement?.audioSources.last != source) {
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
Duration? duration = await audioPlayer.play();
|
||||
await Future.delayed(duration!);
|
||||
if (currentAnnouncement?.audioSources.last != source) {
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
}
|
||||
} catch (e) {
|
||||
// Do nothing
|
||||
// print("Error playing announcement: $e on ${currentAnnouncement?.displayText}");
|
||||
await Future.delayed(const Duration(seconds: 1));
|
||||
}
|
||||
}
|
||||
audioPlayer.stop();
|
||||
// audioPlayer.stop();
|
||||
|
||||
} catch (e) {
|
||||
// Do nothing
|
||||
print("Error playing announcement: $e");
|
||||
}
|
||||
// } catch (e) {
|
||||
// // Do nothing
|
||||
// print("Error playing announcement: $e on ${currentAnnouncement?.displayTex}");
|
||||
// }
|
||||
} else {
|
||||
if (queue.isNotEmpty) {
|
||||
await Future.delayed(const Duration(seconds: 5));
|
||||
@@ -153,7 +188,7 @@ class AnnouncementModule extends InfoModule {
|
||||
}
|
||||
|
||||
// Cache the announcements
|
||||
await announcementCache.loadAnnouncements(audioNames);
|
||||
await announcementCache.loadAnnouncementsFromBytes((await getBundleBytes())!, audioNames);
|
||||
|
||||
List<AudioWrapperSource> sources = [];
|
||||
|
||||
@@ -227,7 +262,7 @@ class AnnouncementModule extends InfoModule {
|
||||
|
||||
String audioRoute = "R_${routeVariant.busRoute.routeNumber}_001.mp3";
|
||||
|
||||
await announcementCache.loadAnnouncements([audioRoute, "R_RAIL_REPLACEMENT_SERVICE_001.mp3"]);
|
||||
await announcementCache.loadAnnouncementsFromBytes(await getBundleBytes(), [audioRoute, "R_RAIL_REPLACEMENT_SERVICE_001.mp3"]);
|
||||
|
||||
AudioWrapperSource sourceRoute = !routeNumber.toLowerCase().startsWith("ul") ?
|
||||
AudioWrapperByteSource(announcementCache[audioRoute]!) :
|
||||
|
||||
@@ -15,14 +15,17 @@ class SyncedTimeModule extends InfoModule {
|
||||
}
|
||||
|
||||
Timer refreshTimer() => Timer.periodic(const Duration(seconds: 10), (timer) async {
|
||||
var res = await http.get(Uri.parse('http://worldtimeapi.org/api/timezone/Europe/London'));
|
||||
if (res.statusCode == 200) {
|
||||
var json = jsonDecode(res.body);
|
||||
DateTime time = DateTime.parse(json['datetime']);
|
||||
timeOffset = time.millisecondsSinceEpoch - DateTime.now().millisecondsSinceEpoch;
|
||||
lastUpdate = DateTime.now();
|
||||
print("Time offset: $timeOffset");
|
||||
} else {
|
||||
|
||||
try {
|
||||
var res = await http.get(Uri.parse('http://worldtimeapi.org/api/timezone/Europe/London'));
|
||||
if (res.statusCode == 200) {
|
||||
var json = jsonDecode(res.body);
|
||||
DateTime time = DateTime.parse(json['datetime']);
|
||||
timeOffset = time.millisecondsSinceEpoch - DateTime.now().millisecondsSinceEpoch;
|
||||
lastUpdate = DateTime.now();
|
||||
print("Time offset: $timeOffset");
|
||||
}
|
||||
} catch (e) {
|
||||
print("Failed to get time from worldtimeapi.org");
|
||||
}
|
||||
});
|
||||
|
||||
@@ -8,25 +8,29 @@ import 'package:bus_infotainment/backend/modules/info_module.dart';
|
||||
import 'package:bus_infotainment/tfl_datasets.dart';
|
||||
import 'package:bus_infotainment/utils/OrdinanceSurveyUtils.dart';
|
||||
import 'package:bus_infotainment/utils/audio%20wrapper.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:geolocator/geolocator.dart';
|
||||
import 'package:vector_math/vector_math.dart';
|
||||
|
||||
class TrackerModule extends InfoModule {
|
||||
|
||||
|
||||
// Constructor
|
||||
TrackerModule() {
|
||||
locationStream();
|
||||
Geolocator.getLastKnownPosition().then((Position? position) {
|
||||
this._position = position;
|
||||
updateNearestStop();
|
||||
});
|
||||
if (!kIsWeb)
|
||||
{
|
||||
Geolocator.getLastKnownPosition().then((Position? position) {
|
||||
this._position = position;
|
||||
updateNearestStop();
|
||||
});
|
||||
}
|
||||
liveInformation.routeVariantDelegate.addListener((routeVariant) {
|
||||
print("Route variant changed");
|
||||
updateNearestStop();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// Location Tracker - will update the recorded location when the user moves.
|
||||
Position? _position;
|
||||
Position? get position => _position;
|
||||
|
||||
@@ -45,6 +49,7 @@ class TrackerModule extends InfoModule {
|
||||
updateNearestStop();
|
||||
});
|
||||
|
||||
// Location Refresher - will update the recorded location periodically.
|
||||
Timer refreshTimer() => Timer.periodic(Duration(seconds: 1), (timer) async {
|
||||
_position = await Geolocator.getCurrentPosition();
|
||||
});
|
||||
@@ -52,6 +57,7 @@ class TrackerModule extends InfoModule {
|
||||
BusRouteStop? nearestStop;
|
||||
bool hasArrived = false;
|
||||
|
||||
|
||||
Future<void> updateNearestStop() async {
|
||||
if (liveInformation.getRouteVariant() == null) {
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user