From 2104f5400b085364827963b133e45f6c6a26434d Mon Sep 17 00:00:00 2001 From: Dominik Kundel Date: Tue, 25 Mar 2025 12:11:33 -0700 Subject: [PATCH] fix(examples): make sure audio playback finishes --- examples/voice/static/main.py | 5 +++++ examples/voice/static/util.py | 1 + 2 files changed, 6 insertions(+) diff --git a/examples/voice/static/main.py b/examples/voice/static/main.py index 4e3840fc..1b9e2024 100644 --- a/examples/voice/static/main.py +++ b/examples/voice/static/main.py @@ -1,6 +1,8 @@ import asyncio import random +import numpy as np + from agents import Agent, function_tool from agents.extensions.handoff_prompt import prompt_with_handoff_instructions from agents.voice import ( @@ -78,6 +80,9 @@ async def main(): elif event.type == "voice_stream_event_lifecycle": print(f"Received lifecycle event: {event.event}") + # Add 1 second of silence to the end of the stream to avoid cutting off the last audio. + player.add_audio(np.zeros(24000 * 1, dtype=np.int16)) + if __name__ == "__main__": asyncio.run(main()) diff --git a/examples/voice/static/util.py b/examples/voice/static/util.py index 455bafee..a5806f41 100644 --- a/examples/voice/static/util.py +++ b/examples/voice/static/util.py @@ -62,6 +62,7 @@ def __enter__(self): return self def __exit__(self, exc_type, exc_value, traceback): + self.stream.stop() # wait for the stream to finish self.stream.close() def add_audio(self, audio_data: npt.NDArray[np.int16]):