Skip to content

Commit d1e2581

Browse files
Let speak instructions without route progress. (#32)
* Let speak instructions without route progress. * Fixes typo and extends changelog.
1 parent 039bedf commit d1e2581

File tree

4 files changed

+23
-28
lines changed

4 files changed

+23
-28
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Changes to the Mapbox Navigation SDK for iOS
22

33
## Unreleased
4+
- The `speak` method in `RouteVoiceController` can be used without a given `RouteProgress` or the `RouteProgress` can explicitly ignored so that it will not be added to the voice instruction.
5+
- `RouteProgress` is now optional in `willSpeak` method of `VoiceControllerDelegate` if the `RouteProgress` in the `speak` method of the `RouteVoiceController is `nil`.
46
- Uses the `Locale` given in `RouteOptions` to create the corresponding `AVSpeechSynthesisVoice`.
57
- Removed setCamera() from updateCourseTracking()
68
- Added setCamera() to progressDidChange()

Examples/Swift/ViewController.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ extension ViewController: VoiceControllerDelegate {
360360
print(interruptedInstruction.text, interruptingInstruction.text)
361361
}
362362

363-
func voiceController(_ voiceController: RouteVoiceController, willSpeak instruction: SpokenInstruction, routeProgress: RouteProgress) -> SpokenInstruction? {
363+
func voiceController(_ voiceController: RouteVoiceController, willSpeak instruction: SpokenInstruction, routeProgress: RouteProgress?) -> SpokenInstruction? {
364364
return SpokenInstruction(distanceAlongStep: instruction.distanceAlongStep, text: "New Instruction!", ssmlText: "<speak>New Instruction!</speak>")
365365
}
366366

MapboxNavigation/RouteVoiceController.swift

+19-26
Original file line numberDiff line numberDiff line change
@@ -192,13 +192,9 @@ open class RouteVoiceController: NSObject, AVSpeechSynthesizerDelegate {
192192

193193
- parameter instruction: The instruction to read aloud.
194194
- 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.
195+
- parameter ignoreProgress: A `Bool` that indicates if the routeProgress is added to the instruction.
195196
*/
196-
open func speak(_ instruction: SpokenInstruction, with locale: Locale?) {
197-
guard let routeProgress else {
198-
assertionFailure("routeProgress should not be nil.")
199-
return
200-
}
201-
197+
open func speak(_ instruction: SpokenInstruction, with locale: Locale?, ignoreProgress: Bool = false) {
202198
if speechSynth.isSpeaking, let lastSpokenInstruction = lastSpokenInstruction {
203199
voiceControllerDelegate?.voiceController?(self, didInterrupt: lastSpokenInstruction, with: instruction)
204200
}
@@ -209,29 +205,26 @@ open class RouteVoiceController: NSObject, AVSpeechSynthesizerDelegate {
209205
voiceControllerDelegate?.voiceController?(self, spokenInstructionsDidFailWith: error)
210206
}
211207

212-
var utterance: AVSpeechUtterance?
208+
let modifiedInstruction = voiceControllerDelegate?.voiceController?(self, willSpeak: instruction, routeProgress: routeProgress) ?? instruction
209+
210+
let utterance: AVSpeechUtterance
211+
213212
if locale?.identifier == "en-US" {
214213
// Alex can’t handle attributed text.
215-
utterance = AVSpeechUtterance(string: instruction.text)
216-
utterance!.voice = AVSpeechSynthesisVoice(identifier: AVSpeechSynthesisVoiceIdentifierAlex)
217-
}
218-
219-
let modifiedInstruction = voiceControllerDelegate?.voiceController?(self, willSpeak: instruction, routeProgress: routeProgress) ?? instruction
220-
221-
if #available(iOS 10.0, *), utterance?.voice == nil {
222-
utterance = AVSpeechUtterance(attributedString: modifiedInstruction.attributedText(for: routeProgress.currentLegProgress))
223-
} else {
224214
utterance = AVSpeechUtterance(string: modifiedInstruction.text)
215+
utterance.voice = AVSpeechSynthesisVoice(identifier: AVSpeechSynthesisVoiceIdentifierAlex)
216+
} else {
217+
if #available(iOS 10.0, *), !ignoreProgress, let routeProgress {
218+
utterance = AVSpeechUtterance(attributedString: modifiedInstruction.attributedText(for: routeProgress.currentLegProgress))
219+
} else {
220+
utterance = AVSpeechUtterance(string: modifiedInstruction.text)
221+
}
222+
223+
// Only localized languages will have a proper fallback voice
224+
utterance.voice = AVSpeechSynthesisVoice(language: locale?.identifier ?? Locale.preferredLocalLanguageCountryCode)
225225
}
226226

227-
// Only localized languages will have a proper fallback voice
228-
if utterance?.voice == nil {
229-
utterance?.voice = AVSpeechSynthesisVoice(language: locale?.identifier ?? Locale.preferredLocalLanguageCountryCode)
230-
}
231-
232-
if let utterance = utterance {
233-
speechSynth.speak(utterance)
234-
}
227+
speechSynth.speak(utterance)
235228
}
236229
}
237230

@@ -264,8 +257,8 @@ public protocol VoiceControllerDelegate {
264257

265258
- parameter voiceController: The voice controller that will speak an instruction.
266259
- parameter instruction: The spoken instruction that will be said.
267-
- parameter routeProgress: The `RouteProgress` just before when the instruction is scheduled to be spoken.
260+
- parameter routeProgress: The `RouteProgress` just before when the instruction is scheduled to be spoken. Could be `nil` if no progress is available or if it should be ignored.
268261
**/
269262
@objc(voiceController:willSpeakSpokenInstruction:routeProgress:)
270-
optional func voiceController(_ voiceController: RouteVoiceController, willSpeak instruction: SpokenInstruction, routeProgress: RouteProgress) -> SpokenInstruction?
263+
optional func voiceController(_ voiceController: RouteVoiceController, willSpeak instruction: SpokenInstruction, routeProgress: RouteProgress?) -> SpokenInstruction?
271264
}

MapboxNavigationTests/NavigationViewControllerTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ class TestableDayStyle: DayStyle {
286286

287287

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

0 commit comments

Comments
 (0)