Control Interruption of Alexa's Speech
When you include output speech in your skill's response to Alexa, you can control whether the new speech interrupts the speech that Alexa is currently saying. This topic describes why you might want to control this behavior, and the attribute you use to do so.
Overview
There are times when you might want Alexa to interrupt what Alexa is currently saying. For example, a lighted push-button gadget might have an accompanying trivia skill. When a user buzzes in while Alexa is asking a question, you might want Alexa to stop asking the question and interrupt herself with "Player 1, what's your answer?"
Other times, you might want Alexa to continue speaking before Alexa says the new speech. Continuing the previous example, a trivia skill might begin by asking all players to push their button as a type of "roll call." Alexa responds to each button press by saying, "Hello, player [n]." In this case, you want Alexa to finish saying hello to player 1 before saying hello to player 2.
For skills that support the CustomInterfaceController
interface, the default behavior is for Alexa to interrupt herself. For all other skills, the default is to not interrupt. You can change this behavior, as described next.
How to control speech interruption
The outputSpeech
object within your skill's response to Alexa provides an optional attribute called playBehavior
, which controls whether the new speech interrupts Alexa's current speech.
You can set playBehavior
to one of the following three values:
ENQUEUE
: Do not interrupt Alexa's current speech. Instead, add this speech to the end of the queue. This is the default value for all skills that do not use theCustomInterfaceController
interface.REPLACE_ALL
: Immediately begin playback of this speech, and replace any current and enqueued speech. This is the default value for all skills that use theCustomInterfaceController
interface.REPLACE_ENQUEUED
: Do not interrupt Alexa's current speech. Replace all speech in the queue with this speech.
If the default behavior is already the behavior that you want, you don't need to set playBehavior
in the response.
Examples
This section provides examples of responses that use the playBehavior
attribute to control whether Alexa interrupts what Alexa is currently saying.
The following SDK versions support setting the playBehavior
attribute:
- Node.js SDK v2.4.0 and higher
- Java SDK v2.11.3 and higher
- Python SDK v1.7.0 and higher
Interrupt speech example
The following response contains speech that interrupts what Alexa is currently saying, and completely replaces any speech that is currently in the queue.
Note that if your skill supports the CustomInterfaceController
interface, you do not need to set the playBehavior
attribute because REPLACE_ALL
is the default setting for skills that support the CustomInterfaceController
interface.
{
"outputSpeech": {
"type": "SSML",
"ssml": "<speak>This speech will interrupt any speech that Alexa is currently saying."/></speak>"
},
"shouldEndSession": false,
"playBehavior": "REPLACE_ALL",
"directives": [ ]
}
This sample code uses the Alexa Skills Kit SDK for Node.js (v2).
const Alexa = require('ask-sdk-core');
const MyIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'MyIntent';
},
handle(handlerInput) {
const speechText = "This speech will interrupt any speech that Alexa is currently saying.";
const playBehavior = "REPLACE_ALL";
// Assemble a response
const response = handlerInput.responseBuilder
.speak(speechText, playBehavior)
.withShouldEndSession(false)
.getResponse();
// Write the response to CloudWatch
console.log("===RESPONSE=== "+ JSON.stringify(response));
// Return the response
return response;
}
};
This sample code uses the Alexa Skills Kit SDK for Java.
package com.amazon.ask.buttongadget.handlers;
import com.amazon.ask.dispatcher.request.handler.HandlerInput;
import com.amazon.ask.dispatcher.request.handler.RequestHandler;
import com.amazon.ask.model.Response;
import static com.amazon.ask.request.Predicates.intentName;
import java.util.Optional;
import com.amazon.ask.model.ui.PlayBehavior;
public class MyIntentHandler implements RequestHandler {
@Override
public boolean canHandle(HandlerInput handlerInput) {
return handlerInput.matches(intentName("MyIntent"));
}
@Override
public Optional<Response> handle(HandlerInput handlerInput) {
// Assemble the response.
Optional<Response> response = handlerInput.getResponseBuilder()
.withSpeech("This speech will interrupt any speech that "
+ "Alexa is currently saying.",
PlayBehavior.REPLACE_ALL)
.withShouldEndSession(false)
.build();
// Write the response to CloudWatch.
String responseLog = response.toString();
responseLog = responseLog.replace("\n", " ").replace("\r", " ");
System.out.println("===RESPONSE=== " + responseLog);
// Return the response.
return response;
}
}
This sample code uses the Alexa Skills Kit SDK for Python.
from ask_sdk_core.dispatch_components import AbstractRequestHandler
from ask_sdk_core.utils import is_intent_name
from ask_sdk_core.handler_input import HandlerInput
from ask_sdk_model.ui import PlayBehavior
from ask_sdk_model import Response
class MyIntentHandler(AbstractRequestHandler):
"""Handler for Hello World Intent."""
def can_handle(self, handler_input):
# type: (HandlerInput) -> bool
return is_intent_name("MyIntent")(handler_input)
def handle(self, handler_input):
# type: (HandlerInput) -> Response
# Assemble the response
speech_text = ("This speech will interrupt any speech "
"that Alexa is currently saying.")
play_behavior = PlayBehavior.REPLACE_ALL
handler_input.response_builder.speak(
speech=speech_text,
play_behavior=play_behavior).set_should_end_session(
False)
# Write response to CloudWatch
response = handler_input.response_builder.response
print(response)
# Return the response
return response
Enqueue speech example
The following response contains speech that does not interrupt what Alexa is currently saying. It adds the speech to the end of the queue.
Note that if your skill does not support the CustomInterfaceController
interface, you do not need to set the playBehavior
attribute because ENQUEUE
is the default setting for skills that do not support the CustomInterfaceController
interface.
{
"outputSpeech": {
"type": "SSML",
"ssml": "<speak>This speech will not interrupt Alexa's speech. It will be added to the queue."/></speak>"
},
"shouldEndSession": false,
"playBehavior": "ENQUEUE",
"directives": [ ]
}
This sample code uses the Alexa Skills Kit SDK for Node.js (v2).
const Alexa = require('ask-sdk-core');
const MyIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'MyIntent';
},
handle(handlerInput) {
const speechText = "This speech will not interrupt Alexa's speech. "
+ "It will be added to the queue.";
const playBehavior = "ENQUEUE";
// Assemble a response
const response = handlerInput.responseBuilder
.speak(speechText, playBehavior)
.withShouldEndSession(false)
.getResponse();
// Write the response to CloudWatch
console.log("===RESPONSE=== "+ JSON.stringify(response));
// Return the response
return response;
}
};
This sample code uses the Alexa Skills Kit SDK for Java.
package com.amazon.ask.buttongadget.handlers;
import com.amazon.ask.dispatcher.request.handler.HandlerInput;
import com.amazon.ask.dispatcher.request.handler.RequestHandler;
import com.amazon.ask.model.Response;
import static com.amazon.ask.request.Predicates.intentName;
import java.util.Optional;
import com.amazon.ask.model.ui.PlayBehavior;
public class MyIntentHandler implements RequestHandler {
@Override
public boolean canHandle(HandlerInput handlerInput) {
return handlerInput.matches(intentName("MyIntent"));
}
@Override
public Optional<Response> handle(HandlerInput handlerInput) {
// Assemble the response.
Optional<Response> response = handlerInput.getResponseBuilder()
.withSpeech("This speech will not interrupt Alexa's speech. "
+ "It will be added to the queue.",
PlayBehavior.ENQUEUE)
.withShouldEndSession(false)
.build();
// Write the response to CloudWatch.
String responseLog = response.toString();
responseLog = responseLog.replace("\n", " ").replace("\r", " ");
System.out.println("===RESPONSE=== " + responseLog);
// Return the response.
return response;
}
}
This sample code uses the Alexa Skills Kit SDK for Python.
from ask_sdk_core.dispatch_components import AbstractRequestHandler
from ask_sdk_core.utils import is_intent_name
from ask_sdk_core.handler_input import HandlerInput
from ask_sdk_model.ui import PlayBehavior
from ask_sdk_model import Response
class MyIntentHandler(AbstractRequestHandler):
"""Handler for Hello World Intent."""
def can_handle(self, handler_input):
# type: (HandlerInput) -> bool
return is_intent_name("MyIntent")(handler_input)
def handle(self, handler_input):
# type: (HandlerInput) -> Response
# Assemble the response
speech_text = ("This speech will not interrupt Alexa's speech. "
"It will be added to the queue.")
play_behavior = PlayBehavior.ENQUEUE
handler_input.response_builder.speak(
speech=speech_text,
play_behavior=play_behavior).set_should_end_session(
False)
# Write response to CloudWatch
response = handler_input.response_builder.response
print(response)
# Return the response
return response
Replace enqueued speech example
The following response contains speech that does not interrupt what Alexa is currently saying. However, it does replace any enqueued speech with the new speech.
{
"outputSpeech": {
"type": "SSML",
"ssml": "<speak>This speech will not interrupt any speech that Alexa is currently saying, but it will replace any speech that's in the queue."/></speak>"
},
"shouldEndSession": false,
"playBehavior": "REPLACE_ENQUEUED",
"directives": [ ]
}
This sample code uses the Alexa Skills Kit SDK for Node.js (v2).
const Alexa = require('ask-sdk-core');
const MyIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'MyIntent';
},
handle(handlerInput) {
const speechText = "This speech will not interrupt any speech "
+ "that Alexa is currently saying, but it "
+ "will replace any speech that's in the queue.";
const playBehavior = "REPLACE_ENQUEUED";
// Assemble a response
const response = handlerInput.responseBuilder
.speak(speechText, playBehavior)
.withShouldEndSession(false)
.getResponse();
// Write the response to CloudWatch
console.log("===RESPONSE=== "+ JSON.stringify(response));
// Return the response
return response;
}
};
This sample code uses the Alexa Skills Kit SDK for Java.
package com.amazon.ask.buttongadget.handlers;
import com.amazon.ask.dispatcher.request.handler.HandlerInput;
import com.amazon.ask.dispatcher.request.handler.RequestHandler;
import com.amazon.ask.model.Response;
import static com.amazon.ask.request.Predicates.intentName;
import java.util.Optional;
import com.amazon.ask.model.ui.PlayBehavior;
public class MyIntentHandler implements RequestHandler {
@Override
public boolean canHandle(HandlerInput handlerInput) {
return handlerInput.matches(intentName("MyIntent"));
}
@Override
public Optional<Response> handle(HandlerInput handlerInput) {
// Assemble the response.
Optional<Response> response = handlerInput.getResponseBuilder()
.withSpeech("This speech will not interrupt any speech that "
+ "Alexa is currently saying, but it will replace "
+ "any speech that's in the queue.",
PlayBehavior.REPLACE_ENQUEUED)
.withShouldEndSession(false)
.build();
// Write the response to CloudWatch.
String responseLog = response.toString();
responseLog = responseLog.replace("\n", " ").replace("\r", " ");
System.out.println("===RESPONSE=== " + responseLog);
// Return the response.
return response;
}
}
This sample code uses the Alexa Skills Kit SDK for Python.
from ask_sdk_core.dispatch_components import AbstractRequestHandler
from ask_sdk_core.utils import is_intent_name
from ask_sdk_core.handler_input import HandlerInput
from ask_sdk_model.ui import PlayBehavior
from ask_sdk_model import Response
class MyIntentHandler(AbstractRequestHandler):
"""Handler for Hello World Intent."""
def can_handle(self, handler_input):
# type: (HandlerInput) -> bool
return is_intent_name("MyIntent")(handler_input)
def handle(self, handler_input):
# type: (HandlerInput) -> Response
# Assemble the response
speech_text = ("This speech will not interrupt any speech "
"that Alexa is currently saying, but it will "
"replace any speech that's in the queue.")
play_behavior = PlayBehavior.REPLACE_ENQUEUED
handler_input.response_builder.speak(
speech=speech_text,
play_behavior=play_behavior).set_should_end_session(
False)
# Write response to CloudWatch
response = handler_input.response_builder.response
print(response)
# Return the response
return response
Last updated: Feb 14, 2022