I’m using streamlit as as a voice assistent where the messages between the user and the LLM is played back using st.markdown since there is currently no autoplay option for st.audio. The problem I have is that if a message is already being played I want to wait until that is finished before playing the next message. Any ideas how to solve this as I have been trying to get it to work without any success.
This is the function I call everytime there is a new meessage.
If you visualize all the audios in the folder in this way:
import streamlit as st
import base64
from queue import Queue
# Initialize a queue to manage the playback of audio files
audio_queue = Queue()
# Function to play audio files sequentially
def autoplay_audio(audio_file_name: str):
# Add the audio file name to the queue
audio_queue.put(audio_file_name)
# If this is the only audio file in the queue, start playback
if audio_queue.qsize() == 1:
play_next_audio()
# Function to play the next audio file in the queue
def play_next_audio():
# If the queue is not empty
if not audio_queue.empty():
# Get the next audio file name from the queue
audio_file_name = audio_queue.get()
file_path = f'D:/Users/ES000881/Desktop/GAP/audio/{audio_file_name}.mp3'
# Read the audio file and convert it to base64
with open(file_path, "rb") as f:
data = f.read()
b64 = base64.b64encode(data).decode()
# Display the audio player using st.audio
st.audio(data, format='audio/mp3')
# Remove the played audio file from the queue
audio_queue.task_done()
# If there are more audio files in the queue, play the next one
if not audio_queue.empty():
play_next_audio()
# Example usage:
autoplay_audio("audio1")
autoplay_audio("audio2")
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.