Skip to content

Voice Downloading

In this guide you will learn how to list all navigation instruction voices available on the server, indicating which are already downloaded, and download a voice.

Setup

First, get an API key token, see the Getting Started guide.
Download the Maps & Navigation SDK for Android archive file

Download the VoiceDownloading project archive file or clone the project with Git

See the Configure Android Example guide.

Run the example

In Android Studio, from the File menu, select Sync Project with Gradle Files

Range finder example Android screenshot

Range finder example Android screenshot

Range finder example Android screenshot

An android device should be connected via USB cable.
Press SHIFT+F10 to compile, install and run the example on the android device.

How it works

Android example screenshot

You can open the MainActivity.kt file to see how the list of navigation instruction voices is obtained from the server, displayed, and how a voice is downloaded.

 1SdkSettings.onConnectionStatusUpdated = { connected ->
 2    if (connected && !voicesCatalogRequested)
 3    {
 4        voicesCatalogRequested = true
 5
 6        val loadVoicesCatalog = {
 7        SdkCall.execute {
 8            // Defines an action that should be done after the network is connected.
 9            // Call to the content store to asynchronously retrieve the list of voices.
10            contentStore.asyncGetStoreContentList(EContentType.HumanVoice, progressListener)
11            }
12        }
The MainActivity overrides the onCreate function, which sends an asynchronous request to the server, once a connection is available, to load the list of available voices. In the request to get the voices from the server, a progressListener is also passed in:
contentStore.asyncGetStoreContentList(EContentType.HumanVoice, progressListener)
 1private val progressListener = ProgressListener.create(
 2     onStarted = {
 3         progressBar?.visibility = View.VISIBLE
 4         showStatusMessage("Started content store service.")
 5     },
 6     onCompleted = { errorCode, _ ->
 7         progressBar?.visibility = View.GONE
 8         showStatusMessage("Content store service completed with error code: $errorCode")
 9         when (errorCode)
10         {
11             GemError.NoError ->
12             {
13                 // No error encountered, we can handle the results.
14                 SdkCall.execute {
15
16                 // Get the list of voices that was retrieved in the content store.
17                 val models = contentStore.getStoreContentList(EContentType.HumanVoice)?.first
18
19                 if (!models.isNullOrEmpty())
20                 {
21                     // The voice items list is not empty or null.
22                     val voiceItem = models[0]
23                     val itemName = voiceItem.name
24                     // Start downloading the first voice item.
25                     SdkCall.execute {
26                     voiceItem.asyncDownload(GemSdk.EDataSavePolicy.UseDefault,
27                         true,
28                         onStarted = {
29                             showStatusMessage("Started downloading $itemName.")
30                         },
31                         onCompleted = { _, _ ->
32                             listView?.adapter?.notifyItemChanged(0)
33                             showStatusMessage("$itemName was downloaded.")
34                         },
35                         onProgress = {
36                             listView?.adapter?.notifyItemChanged(0)
37                         })
38                     }
39                 }
40                 displayList(models)
41                 }
42             }
43             GemError.Cancel ->
44             {
45                 // The action was cancelled.
46             }
47             else ->
48             {
49                 // There was a problem at retrieving the content store items.
50                 showDialog("Content store service error: ${GemError.getMessage(errorCode)}")
51             }
52         }
53     },
54     postOnMain = true
55)
In MainActivity a progressListener is created to wait for the online content store service to start. The onCompleted block in the progress listener waits for the content store service to be ready.
If there was no error in obtaining the list of voices from the server, requested from the onCreate function, the progress listener gets the list of voices from the downloaded (local) copy of the list:
val models = contentStore.getStoreContentList(EContentType.HumanVoice)?.first
If the list of voices, named models, is not empty, then the data (sound) for the first voice in the list, at index 0, is downloaded automatically:
val voiceItem = models[0]
voiceItem.asyncDownload()
1private fun displayList(models: ArrayList<ContentStoreItem>?)
2{
3    if (models != null)
4    {
5        val adapter = CustomAdapter(models)
6        listView?.adapter = adapter
7    }
8}
The example also uses a CustomAdapter to display a scrollable list of country names, flags and voice names with the displayList() function.

Android Examples

Maps SDK for Android Examples can be downloaded or cloned with Git