Text-to-Speech
The Navigation SDK provides built-in voice guidance starting from version 5.6.0. When enabled, the SDK handles all voice synthesis internally, eliminating the need for external voice playback management.
Enable Built-in Voice
Use the following interface to enable or disable the built-in voice feature:
/**
* Sets whether to use the built-in voice for navigation guidance.
*
* @param isUseInnerVoice Whether to use the built-in voice. Default is false.
* @param isCallBackText When isUseInnerVoice is true, specifies whether the
* {@link AMapNaviListener#onGetNavigationText} callback
* continues to return text. Default is false.
*/
@Override
public void setUseInnerVoice(boolean isUseInnerVoice, boolean isCallBackText)Control Voice Playback
If you are using the built-in voice, use the following interfaces to control playback behavior:
/**
* Starts built-in voice playback. Only effective when built-in voice is enabled.
* @since 6.4.0
*/
@Override
public void startSpeak()
/**
* Stops built-in voice playback. Only effective when built-in voice is enabled.
*
* Note: After version 7.1.0, calling this method stops navigation voice playback
* but still allows custom voice playback.
* @since 6.4.0
*/
@Override
public void stopSpeak()Receive Navigation Text
The SDK provides the navigation text through the AMapNaviListener callback. If you prefer not to use the built-in voice, you can use a third-party text-to-speech (TTS) SDK,to convert the text into audio.
/**
* Callback for navigation voice prompts.
*
* @param text The text to be spoken.
* @since 5.3.0
*/
void onGetNavigationText(String text)Check Playback Status
When using a custom TTS solution, call the following method before and after each utterance to avoid voice interruptions and delays:
/**
* Checks whether the SDK is currently playing voice.
*
* Important: Do not play voice while the SDK is already speaking,
* as this may cause conflicts with navigation prompts.
*
* @since 6.4.0
* @return true if the SDK is currently playing voice, false otherwise.
*/
public static boolean isTtsPlaying()Custom Voice Playback
If you are using the built-in voice, you can also play custom text through the SDK's voice synthesis:
/**
* Plays custom text. Only effective when built-in voice is enabled.
*
* Note: If navigation voice is currently playing, playback may fail.
*
* @param tts The text to play.
* @param forcePlay Whether to force playback.
* true - Waits for current navigation voice to finish,
* then plays the custom text. May cause loss of
* critical navigation guidance.
* false - Does not play if navigation voice is currently active.
* @return true if playback was successful, false otherwise.
* @since 6.7.0
*/
@Override
public boolean playTTS(String tts, boolean forcePlay)Configure Local Voice Pack
In addition to the default voice, you can configure a local voice pack for playback. Note that Chinese voice guidance only supports local voice packs, and the standard voice mode is not available.
Set Language, Voice Mode, and Local Voice Pack Path
Use the AMapNaviAppLangOptions class to configure these settings:
public class AMapNaviAppLangOptions {
private int appLangType = 2;
private String ttsVoicePath = "";
private String speakerId;
private AudioOutputMode audioOutputMode;
public AMapNaviAppLangOptions() {
this.audioOutputMode = AudioOutputMode.AMapNaviAudioOutputModeNone;
}
public int getAppLangType() {
return this.appLangType;
}
public void setAppLangType(int var1) {
this.appLangType = var1;
}
public String getTtsVoicePath() {
return this.ttsVoicePath;
}
public void setTtsVoicePath(String var1) {
this.ttsVoicePath = var1;
}
public String getSpeakerId() {
return this.speakerId;
}
public void setSpeakerId(String var1) {
this.speakerId = var1;
}
public AudioOutputMode getAudioOutputMode() {
return this.audioOutputMode;
}
public void setAudioOutputMode(AudioOutputMode var1) {
this.audioOutputMode = var1;
}
}Apply the configuration:
public static void setAppLang(android.content.Context context, com.amap.api.navi.AMapNaviAppLangOptions aMapNaviAppLangOptions, com.amap.api.navi.AudioAuthTaskCallback audioAuthTaskCallback) {}
public static int getAppLang() {}