1+ /*
2+ This code example is part of the Mapbox Navigation SDK for iOS demo app.
3+ To learn more about each example in this app, including descriptions and links
4+ to documentation, see our docs: https://docs.mapbox.com/ios/navigation/examples/custom-voice-controller/
5+ */
6+
17import Foundation
28import UIKit
39import MapboxCoreNavigation
@@ -20,33 +26,47 @@ class CustomVoiceControllerUI: UIViewController {
2026 case . failure( let error) :
2127 print ( error. localizedDescription)
2228 case . success( let response) :
23- guard let strongSelf = self else {
24- return
25- }
26-
27- // For demonstration purposes, simulate locations if the Simulate Navigation option is on.
28- let navigationService = MapboxNavigationService ( routeResponse: response, routeIndex: 0 , routeOptions: routeOptions, simulating: simulationIsEnabled ? . always : . onPoorGPS)
29-
30- // `MultiplexedSpeechSynthesizer` will provide "a backup" functionality to cover cases, which
31- // our custom implementation cannot handle.
32- let speechSynthesizer = MultiplexedSpeechSynthesizer ( [ CustomVoiceController ( ) , SystemSpeechSynthesizer ( ) ] as? [ SpeechSynthesizing ] )
33- let routeVoiceController = RouteVoiceController ( navigationService: navigationService, speechSynthesizer: speechSynthesizer)
34- // Remember to pass our `Voice Controller` to `Navigation Options`!
35- let navigationOptions = NavigationOptions ( navigationService: navigationService, voiceController: routeVoiceController)
36-
37- let navigationViewController = NavigationViewController ( for: response, routeIndex: 0 , routeOptions: routeOptions, navigationOptions: navigationOptions)
38- navigationViewController. modalPresentationStyle = . fullScreen
39-
40- strongSelf. present ( navigationViewController, animated: true , completion: nil )
29+ self ? . presentNavigationWithCustomVoiceController ( routeOptions: routeOptions, response: response)
4130 }
4231 }
4332 }
33+
34+ func presentNavigationWithCustomVoiceController( routeOptions: NavigationRouteOptions , response: RouteResponse ) {
35+ // For demonstration purposes, simulate locations if the Simulate Navigation option is on.
36+ let navigationService = MapboxNavigationService ( routeResponse: response,
37+ routeIndex: 0 ,
38+ routeOptions: routeOptions,
39+ simulating: simulationIsEnabled ? . always : . onPoorGPS)
40+
41+ // `MultiplexedSpeechSynthesizer` will provide "a backup" functionality to cover cases, which
42+ // our custom implementation cannot handle.
43+ let speechSynthesizer = MultiplexedSpeechSynthesizer ( [ CustomVoiceController ( ) , SystemSpeechSynthesizer ( ) ] )
44+
45+ // Create a `RouteVoiceController` type with a customized `SpeechSynthesizing` instance.
46+ // A route voice controller monitors turn-by-turn navigation events and triggers playing spoken instructions
47+ // as audio using the custom `speechSynthesizer` we created above.
48+ let routeVoiceController = RouteVoiceController ( navigationService: navigationService,
49+ speechSynthesizer: speechSynthesizer)
50+ // Remember to pass our RouteVoiceController` to `Navigation Options`!
51+ let navigationOptions = NavigationOptions ( navigationService: navigationService,
52+ voiceController: routeVoiceController)
53+
54+ // Create `NavigationViewController` with the custom `NavigationOptions`.
55+ let navigationViewController = NavigationViewController ( for: response,
56+ routeIndex: 0 ,
57+ routeOptions: routeOptions,
58+ navigationOptions: navigationOptions)
59+ navigationViewController. modalPresentationStyle = . fullScreen
60+
61+ present ( navigationViewController, animated: true , completion: nil )
62+ }
4463}
4564
4665class CustomVoiceController : MapboxSpeechSynthesizer {
4766
4867 // You will need audio files for as many or few cases as you'd like to handle
49- // This example just covers left and right. All other cases will fail the Custom Voice Controller and force a backup System Speech to kick in
68+ // This example just covers left and right. All other cases will fail the Custom Voice Controller and
69+ // force a backup System Speech to kick in
5070 let turnLeft = NSDataAsset ( name: " turnleft " ) !. data
5171 let turnRight = NSDataAsset ( name: " turnright " ) !. data
5272
0 commit comments