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
data
array is empty,Selector
renders the first component in theitems
array wherewhen
evaluates totrue
. The component uses the single child inflation method. -
When the data array contains items, the
Selector
component iterates thedata
array and checks each component in theitems
array for atrue
when
property for each item in the data array. The data-binding context is extended with thedata
,index
, andlength
properties. TheSelector
component 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 thewhen
clause evaluates totrue.
- If the
data
array is empty, theSelector
component picks and renders the first component in theitems
array where thewhen
clause equalstrue
. - If the
data
array is not empty, theSelector
component checks each component in thecomponents
array in order looking for awhen
clause that equalstrue
in thedata
array. The data-binding context is extended by binding thedata
,index
, andlength
properties. TheSelector
component finishes after rendering a single component and then stops iterating over the data array.
- If the
randomItem
– Render a random item whosewhen
clause evaluates totrue
.- If the
data
array is empty, theSelector
component evaluates thewhen
clauses of all child components and selects one to render where thewhen
clause equalstrue
. - If the
data
array is not empty, theSelector
component performs the random selection operation once per item in the data array in order. The data-binding context is extended by bindingdata
,index
, andlength
properties. TheSelector
component finishes after rendering a single component and then stops iterating over the data array.
- If the
randomData
– Renders either the first item where thewhen
clause evaluates totrue
or an item chosen from a random index in thedata
array:- If the
data
array is empty, theSelector
component picks and renders the first component in theitems
array where thewhen
clause equalstrue
. Similar tonormal
strategy. - If the
data
array is not empty, theSelector
component performs thenormal
strategy on a random index of thedata
array. If no component is rendered, theSelector
performs thenormal
strategy on another random index in thedata
array. This process repeats until either rendering a single component is rendered, or exhausting all items in thedata
array.
- If the
randomItemRandomData
– Uses therandomItem
strategy for an emptydata
array and uses therandomData
strategy for a non-emptydata
array.- If the
data
array is empty, theSelector
component evaluates thewhen
clauses of all child components and selects one to render where thewhen
clause equalstrue
. Similar torandomItem
strategy. - If the
data
array is not empty, theSelector
component performs thenormal
strategy on a random index of thedata
array. If no component is rendered, theSelector
performs thenormal
strategy on another random index in thedata
array. This process repeats until either rendering a single component is rendered, or exhausting all items in thedata
array. Similar to therandomData
strategy.
- 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