Skip to content

fix(examples): make sure audio playback finishes #340

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions examples/voice/static/main.py
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down Expand Up @@ -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())
1 change: 1 addition & 0 deletions examples/voice/static/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]):
Expand Down