Skip to content

Commit

Permalink
Let speak instructions without route progress.
Browse files Browse the repository at this point in the history
  • Loading branch information
StephanPartzsch committed Mar 16, 2024
1 parent 039bedf commit 7f56bd1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
34 changes: 15 additions & 19 deletions MapboxNavigation/RouteVoiceController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,9 @@ open class RouteVoiceController: NSObject, AVSpeechSynthesizerDelegate {

- parameter instruction: The instruction to read aloud.
- parameter locale: The `Locale` used to create the voice read aloud the given instruction. If `nil` the `Locale.preferredLocalLanguageCountryCode` is used for creating the voice.
- parameter ignoreProgress: A `Bool` that indicates if the routeProgress is added to the instruction.
*/
open func speak(_ instruction: SpokenInstruction, with locale: Locale?) {
guard let routeProgress else {
assertionFailure("routeProgress should not be nil.")
return
}

open func speak(_ instruction: SpokenInstruction, with locale: Locale?, ignoreProgress: Bool = false) {
if speechSynth.isSpeaking, let lastSpokenInstruction = lastSpokenInstruction {
voiceControllerDelegate?.voiceController?(self, didInterrupt: lastSpokenInstruction, with: instruction)
}
Expand All @@ -209,29 +205,29 @@ open class RouteVoiceController: NSObject, AVSpeechSynthesizerDelegate {
voiceControllerDelegate?.voiceController?(self, spokenInstructionsDidFailWith: error)
}

var utterance: AVSpeechUtterance?
var utterance = AVSpeechUtterance(string: instruction.text)
if locale?.identifier == "en-US" {
// Alex can’t handle attributed text.
utterance = AVSpeechUtterance(string: instruction.text)
utterance!.voice = AVSpeechSynthesisVoice(identifier: AVSpeechSynthesisVoiceIdentifierAlex)
utterance.voice = AVSpeechSynthesisVoice(identifier: AVSpeechSynthesisVoiceIdentifierAlex)
}

let modifiedInstruction = voiceControllerDelegate?.voiceController?(self, willSpeak: instruction, routeProgress: routeProgress) ?? instruction

if #available(iOS 10.0, *), utterance?.voice == nil {
if
#available(iOS 10.0, *),
!ignoreProgress,
utterance.voice == nil,
let routeProgress
{
let modifiedInstruction = voiceControllerDelegate?.voiceController?(self, willSpeak: instruction, routeProgress: routeProgress) ?? instruction

utterance = AVSpeechUtterance(attributedString: modifiedInstruction.attributedText(for: routeProgress.currentLegProgress))
} else {
utterance = AVSpeechUtterance(string: modifiedInstruction.text)
}

// Only localized languages will have a proper fallback voice
if utterance?.voice == nil {
utterance?.voice = AVSpeechSynthesisVoice(language: locale?.identifier ?? Locale.preferredLocalLanguageCountryCode)
if utterance.voice == nil {
utterance.voice = AVSpeechSynthesisVoice(language: locale?.identifier ?? Locale.preferredLocalLanguageCountryCode)
}

if let utterance = utterance {
speechSynth.speak(utterance)
}
speechSynth.speak(utterance)
}
}

Expand Down
2 changes: 1 addition & 1 deletion MapboxNavigationTests/NavigationViewControllerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ class TestableDayStyle: DayStyle {


class FakeVoiceController: RouteVoiceController {
override func speak(_ instruction: SpokenInstruction, with locale: Locale?) {
override func speak(_ instruction: SpokenInstruction, with locale: Locale?, ignoreProgress: Bool = false) {
//no-op
}

Expand Down

0 comments on commit 7f56bd1

Please sign in to comment.