Skip to main content
GuidesAPI ReferenceExamples

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.

Voices list
Downloaded voices

Getting the list

To get the voices list a ContentStore must be instantiated. Normally in Android's MVVM, stores are retained in the Repository layer, injected via dependency injection, and ultimately consumed by ViewModels but for the scope of this example we will use it inside an activity. To request the list there also needs to be an established internet connection. In the code below is shown how to make a request for the voice list.

MainActivity.kt
class MainActivity : AppCompatActivity()
{
//...
private val contentStore = ContentStore()

private var voicesCatalogRequested = false
//...
override fun onCreate(savedInstanceState: Bundle?)
{
//...
// Defines an action that should be done after the network is connected.
SdkSettings.onConnectionStatusUpdated = { connected ->
if (connected && !voicesCatalogRequested)
{
voicesCatalogRequested = true

//...
SdkCall.execute {
// Call to the content store to asynchronously retrieve the list of voices.
contentStore.asyncGetStoreContentList(
EContentType.HumanVoice,
progressListener
)
}
//...
}
//...

}
}

If retrieving the list is succesfull the ProgressListener.onCompleted callback is triggered with no error. Download is started on the first succesfull retrieved item of the list. The voiceItem.asyncDownload(..) also receives progress callbacks that will update the UI.

MainActivity.kt
class MainActivity : AppCompatActivity()
{
//...

private val progressListener = ProgressListener.create(
onStarted = {
progressBar?.visibility = View.VISIBLE
showStatusMessage("Started content store service.")
},

onCompleted = { errorCode, _ ->
progressBar?.visibility = View.GONE
showStatusMessage("Content store service completed with error code: $errorCode")

when (errorCode)
{
GemError.NoError ->
{
SdkCall.execute {
// No error encountered, we can handle the results.
// Get the list of voices that was retrieved in the content store.

val models =
contentStore.getStoreContentList(EContentType.HumanVoice)?.first
if (!models.isNullOrEmpty())
{
// The voice items list is not empty or null.
val voiceItem = models[0]
val itemName = voiceItem.name

// Start downloading the first voice item.
SdkCall.execute {
voiceItem.asyncDownload(GemSdk.EDataSavePolicy.UseDefault,
true,
onStarted = {
showStatusMessage("Started downloading $itemName.")
},
onCompleted = { _, _ ->
listView?.adapter?.notifyItemChanged(0)
showStatusMessage("$itemName was downloaded.")
},
onProgress = {
listView?.adapter?.notifyItemChanged(0)
})
}
}

displayList(models)
}
}

GemError.Cancel ->
{
// The action was cancelled.
}

else ->
{
// There was a problem at retrieving the content store items.
showDialog("Content store service error: ${GemError.getMessage(errorCode)}")
}
}
},
postOnMain = true
)
}

Finally, the list is displayed using a RecyclerView.Adapter.

MainActivity.kt
private fun displayList(models: ArrayList<ContentStoreItem>?)
{
if (models != null)
{
val adapter = CustomAdapter(models)
listView?.adapter = adapter
}
}

The example also uses a CustomAdapter to display a scrollable list of country names, flags and voice names with the displayList() function.