Код: Выделить всё
Future _initAudioSession() async {
await _flutterTts.setLanguage(isEnglish() ? "en-US" : "nl-NL");
await _flutterTts.setSpeechRate(0.5); // Of wat voor jouw goed klinkt
await _flutterTts.setVolume(1.0);
await _flutterTts.setPitch(1.0);
}
Future synthesizeSpeechToFile(String text) async {
final dir = await getTemporaryDirectory();
final filePath = '${dir.path}/tts_output.wav';
await _flutterTts.synthesizeToFile(text, filePath);
print("🎙️ TTS bestand pad: $filePath");
print("🎧 Bestand bestaat: ${File(filePath).existsSync()}");
return filePath;
}
Future speakInstruction(String instructionText) async {
if (Platform.isIOS) {
// iOS ondersteunt synthesizeToFile niet — speel direct af
try {
await _flutterTts.speak(instructionText);
print('✅ [iOS] TTS gesproken via directe speak()');
} catch (e) {
print('❌ Fout bij TTS speak(): $e');
}
return;
}
// Android: gebruik synthesizeToFile()
final fileName = instructionText.hashCode.toString();
final dir = await getTemporaryDirectory();
final filePath = '${dir.path}/$fileName.aac';
final result = await _flutterTts.synthesizeToFile(
instructionText,
filePath,
);
if (result == 1) {
try {
await _audioPlayer.setSourceDeviceFile(filePath);
await _audioPlayer.play(DeviceFileSource(filePath));
print('✅ [Android] TTS afgespeeld via audiobestand');
} catch (e) {
print('❌ Fout bij afspelen TTS-bestand: $e');
}
} else {
print('❌ TTS bestand niet gegenereerd (result: $result)');
}
}
void _startNavigationInstructionUpdates() {
if (kDebugMode) {
print("🚀 Navigatie gestart, huidige positie: $_currentPosition");
}
_updateLocationAccuracy();
_routeStartTime = DateTime.now();
_loadInterstitialAd();
if (_positionBuffer.length < 2 && _routePoints.length >= 2) {
_positionBuffer = [_routePoints[0], _routePoints[1]];
if (kDebugMode) {
print("🛫 Eerste 2 punten in buffer gezet: ${_positionBuffer.length}");
}
}
const checkInterval = Duration(seconds: 2);
Timer.periodic(checkInterval, (timer) async {
if (!_isNavigating ||
_activeInstructions.isEmpty ||
_currentInstructionIndex >= _activeInstructions.length) {
timer.cancel();
return;
}
final instruction = _activeInstructions[_currentInstructionIndex];
final interval = instruction["interval"];
final distanceFutures = [];
for (int i = interval[0]; i 25) {
extraText = isEnglish() ? " (in 50 meters)" : " (over 50 meter)";
}
_navigationInstruction = "$instructionText$extraText";
_currentNavigationIcon = _getNavigationIconForInstruction(instruction);
int nextIdx = _currentInstructionIndex + 1;
if (nextIdx < _activeInstructions.length) {
final nextInstr = _activeInstructions[nextIdx];
_nextInstructionDirection = nextInstr["text"] ?? "";
_nextInstructionDirectionNl = _nextInstructionDirection;
_nextNavigationIcon = _getNavigationIconForInstruction(nextInstr);
} else {
_nextInstructionDirection = "";
_nextInstructionDirectionNl = "";
_nextNavigationIcon = Icons.arrow_forward;
}
_currentInstructionIndex++;
});
_updateRoadTypeAhead();
}
});
}
Подробнее здесь: https://stackoverflow.com/questions/797 ... lutter-tts