how can i use something like await asyncio.sleep(6...
# ask-ai
a
how can i use something like await asyncio.sleep(6) before ending the call by deleting the room? This works perfectly when using cartesia tts but does not work with elevenlabs. the call ends but it does not play the last speech. I've already tried using the wait_for_playout() method.
t
Thanks for reaching out, Jawad. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To ensure the last speech is played before ending a call when using ElevenLabs TTS, you can use the
wait_for_playout()
method on the
SpeechHandle
. Here’s how: • Generate Speech and Get Handle: Use
session.say()
or
session.generate_reply()
to create a
SpeechHandle
object. Set
allow_interruptions=False
to prevent interruptions during the speech. • Wait for Speech Playout: Call
await handle.wait_for_playout()
to ensure the speech finishes before proceeding. This method blocks until the speech has completed playing. • End the Call: After the speech completes, end the call using the
delete_room
API. This ensures the session terminates only after the last speech is played. Here’s an example:
Copy code
handle = session.say("Thank you for calling. Goodbye!", allow_interruptions=False)
await handle.wait_for_playout()
await ctx.api.room.delete_room(api.DeleteRoomRequest(room=ctx.room.name))
Alternatively, if you prefer using
asyncio.sleep()
, you can poll the speech handle’s
done()
status:
Copy code
handle = session.say("Goodbye message...", allow_interruptions=False)

while not handle.done():
    await asyncio.sleep(0.1)

await ctx.api.room.delete_room(api.DeleteRoomRequest(room=ctx.room.name))
Using
wait_for_playout()
is recommended as it handles the playout asynchronously without busy waiting. Let me know if you need further assistance!