MapboxVoiceController
open class MapboxVoiceController : RouteVoiceController, AVAudioPlayerDelegate
The Mapbox voice controller plays spoken instructions using the MapboxSpeech framework.
You initialize a voice controller using a NavigationService
instance. The voice controller observes when the navigation service hints that the user has passed a spoken instruction point and responds by converting the contents of a SpokenInstruction
object into audio and playing the audio.
The MapboxSpeech framework requires a network connection to connect to the Mapbox Voice API, but it produces superior speech output in several languages including English. If the voice controller is unable to connect to the Voice API, it falls back to the Speech Synthesis framework as implemented by the superclass, RouteVoiceController
. To mitigate network latency over a cell connection, MapboxVoiceController
prefetches and caches synthesized audio.
If you need to supply a third-party speech synthesizer that requires a network connection, define a subclass of MapboxVoiceController
that overrides the speak(_:)
method. If the third-party speech synthesizer does not require a network connection, you can instead subclass RouteVoiceController
.
The Mapbox Voice API is optimized for spoken instructions provided by the Mapbox Directions API via the MapboxDirections.swift framework. If you need text-to-speech functionality outside the context of a navigation service, use the Speech Synthesis framework’s AVSpeechSynthesizer
class directly.
-
Number of seconds a request can wait before it is canceled and the default speech synthesizer speaks the instruction.
Declaration
Swift
public var timeoutIntervalForRequest: TimeInterval
-
Number of steps ahead of the current step to cache spoken instructions.
Declaration
Swift
public var stepsAheadToCache: Int
-
An
AVAudioPlayer
through which spoken instructions are played.Declaration
Swift
public var audioPlayer: AVAudioPlayer?
-
Speaks an instruction.
The cache is first checked to see if we have already downloaded the speech file. If not, the instruction is fetched and played. If there is an error anywhere along the way, the instruction will be spoken with the default speech synthesizer.
Declaration
Swift
open override func speak(_ instruction: SpokenInstruction)
-
Speaks an instruction with the built in speech synthesizer.
This method should be used in cases where
fetch(instruction:)
orplay(_:)
fails.Declaration
Swift
open func speakWithDefaultSpeechSynthesizer(_ instruction: SpokenInstruction, error: SpeechError?)
-
Fetches and plays an instruction.
Declaration
Swift
open func fetchAndSpeak(instruction: SpokenInstruction)
-
Caches an instruction in an in-memory cache.
Declaration
Swift
open func downloadAndCacheSpokenInstruction(instruction: SpokenInstruction)
-
Plays an audio file.
Declaration
Swift
open func play(instruction: SpokenInstruction, data: Data)