Selector (APL for Audio)
A Selector component renders a single audio clip, selected from an array of possible clips. You provide Selector with an array of child components that define each audio clip. The Selector then renders the audio for the first child component where the when property evaluates to true.
Properties
The Selector component has the base component properties and the following component-specific properties:
| Property | Type | Default | Description |
|---|---|---|---|
item(s) |
Array of components | No | The array of child components. |
data |
Array | [ ] | An array of data to map against the component. |
strategy |
String | normal |
The strategy used to select the child. Valid values are normal, randomItem, randomData, randomItemRandomData |
item(s)
The child component to render. If more than one child is supplied, the Selector component renders the first one selected by the when property.
data
An array of data to iterate over and evaluate.
-
When the
dataarray is empty,Selectorrenders the first component in theitemsarray wherewhenevaluates totrue. The component uses the single child inflation method. -
When the data array contains items, the
Selectorcomponent iterates thedataarray and checks each component in theitemsarray for atruewhenproperty for each item in the data array. The data-binding context is extended with thedata,index, andlengthproperties. TheSelectorcomponent finishes when it selects and renders a single component. It doesn't continue iterating over the data array.
During iteration, the data-binding context is extended with the following properties:
| Name | Description |
|---|---|
data |
Data assigned from the data array property |
index |
The 0-based index of the current data item |
length |
The total number of data items in the data array |
These properties are set when the data array property contains at least one item.
strategy
The selection strategy to determine which item to render. The specified strategy determines how the Selector component chooses which item to render.
The strategy values trigger the following behaviors:
normal– Render the first item where thewhenclause evaluates totrue.- If the
dataarray is empty, theSelectorcomponent picks and renders the first component in theitemsarray where thewhenclause equalstrue. - If the
dataarray is not empty, theSelectorcomponent checks each component in thecomponentsarray in order looking for awhenclause that equalstruein thedataarray. The data-binding context is extended by binding thedata,index, andlengthproperties. TheSelectorcomponent finishes after rendering a single component and then stops iterating over the data array.
- If the
randomItem– Render a random item whosewhenclause evaluates totrue.- If the
dataarray is empty, theSelectorcomponent evaluates thewhenclauses of all child components and selects one to render where thewhenclause equalstrue. - If the
dataarray is not empty, theSelectorcomponent performs the random selection operation once per item in the data array in order. The data-binding context is extended by bindingdata,index, andlengthproperties. TheSelectorcomponent finishes after rendering a single component and then stops iterating over the data array.
- If the
randomData– Renders either the first item where thewhenclause evaluates totrueor an item chosen from a random index in thedataarray:- If the
dataarray is empty, theSelectorcomponent picks and renders the first component in theitemsarray where thewhenclause equalstrue. Similar tonormalstrategy. - If the
dataarray is not empty, theSelectorcomponent performs thenormalstrategy on a random index of thedataarray. If no component is rendered, theSelectorperforms thenormalstrategy on another random index in thedataarray. This process repeats until either rendering a single component is rendered, or exhausting all items in thedataarray.
- If the
randomItemRandomData– Uses therandomItemstrategy for an emptydataarray and uses therandomDatastrategy for a non-emptydataarray.- If the
dataarray is empty, theSelectorcomponent evaluates thewhenclauses of all child components and selects one to render where thewhenclause equalstrue. Similar torandomItemstrategy. - If the
dataarray is not empty, theSelectorcomponent performs thenormalstrategy on a random index of thedataarray. If no component is rendered, theSelectorperforms thenormalstrategy on another random index in thedataarray. This process repeats until either rendering a single component is rendered, or exhausting all items in thedataarray. Similar to therandomDatastrategy.
- If the
Examples
Basic Selector
The following example demonstrates a basic Selector component in a document bound to a data source. When ${payload.myData.name} contains an empty string, the Selector renders the first Speech component. Otherwise, the Selector renders the second Speech component, which also includes the data name property.
With the following data source, the Selector creates an audio clip with the speech "Hi, John!"
{
"myData": {
"name": "John"
}
}
If you change the content of the Data Sources tab to the following, the document generates an audio clip with the speech "Hello! I don't yet know your name."
{
"myData": {
"name": ""
}
}
Locale Selector
The following example demonstrates a Selector that uses the user's locale to determine whether to say "hello" in English or Spanish.
{
"type": "Selector",
"items": [
{
"type": "Speech",
"when": "${environment.alexaLocale == 'en-US'}",
"contentType": "PlainText",
"content": "Hello!"
},
{
"type": "Speech",
"when": "${environment.alexaLocale == 'es-ES'}",
"contentType": "SSML",
"content": "<speak><voice name='Conchita'>Hola!</voice></speak>"
}
]
}
When the user's device uses the English (US) locale, the environment.alexaLocale value returns en-US. The Selector generates the speech "Hello!" When the user's device uses the Spanish (Spain) locale, environment.alexaLocale returns es-ES, so the Selector generates the speech "Hola!" In this example, the second Speech component uses SSML to render the speech with the "Conchita" voice.
Related topics
Last updated: Nov 28, 2023