Skip to content

Commit 2104f54

Browse files
fix(examples): make sure audio playback finishes
1 parent ad020b7 commit 2104f54

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

examples/voice/static/main.py

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import asyncio
22
import random
33

4+
import numpy as np
5+
46
from agents import Agent, function_tool
57
from agents.extensions.handoff_prompt import prompt_with_handoff_instructions
68
from agents.voice import (
@@ -78,6 +80,9 @@ async def main():
7880
elif event.type == "voice_stream_event_lifecycle":
7981
print(f"Received lifecycle event: {event.event}")
8082

83+
# Add 1 second of silence to the end of the stream to avoid cutting off the last audio.
84+
player.add_audio(np.zeros(24000 * 1, dtype=np.int16))
85+
8186

8287
if __name__ == "__main__":
8388
asyncio.run(main())

examples/voice/static/util.py

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ def __enter__(self):
6262
return self
6363

6464
def __exit__(self, exc_type, exc_value, traceback):
65+
self.stream.stop() # wait for the stream to finish
6566
self.stream.close()
6667

6768
def add_audio(self, audio_data: npt.NDArray[np.int16]):

0 commit comments

Comments
 (0)