A Streamlit component for sequential audio playback with an internal queue.
Project description
Streamlit Audio Queue Player
This is a custom Streamlit component that plays audio files sequentially. It maintains an internal queue on the frontend so that when you send audio (as a base64 string) from Streamlit, they are played one after another.
Features
- Internal Queue: New audio items are added to a queue and played sequentially.
- Persistent State: The component uses a fixed key so that its internal state is preserved across Streamlit reruns.
- Easy Integration: Simply call
audio_player(audio, format="audio/wav", key="...")from your Streamlit app. - Automatic Refresh Example: An included sample app automatically refreshes every 1 second.
Installation
pip install streamlit-audio-queue-player
Usage
In a Streamlit App A sample Streamlit app is provided under the example/ directory. For example:
streamlit run example/app.py
In your app, you can add audio to the playback queue as follows:
import streamlit as st
import base64
from streamlit_audio_queue_player import audio_player
st.title("Demo: Streamlit Audio Player Component")
# Example: When the button is pressed, a dummy audio file is added to the queue.
if st.button("Add Dummy Audio"):
try:
with open("dummy.wav", "rb") as f:
audio_bytes = f.read()
audio_b64 = base64.b64encode(audio_bytes).decode()
# Call the component with a fixed key so that its internal queue is preserved.
audio_player(audio_b64, format="audio/wav", key="audio_player")
except Exception as e:
st.error(f"Error loading dummy audio: {e}")
else:
# Even if no new audio is provided, call the component to maintain its state.
audio_player(None, format="audio/wav", key="audio_player")
For Forcing Stop / Clearing the Queue
You can force-stop the current audio and clear the playback queue by providing a new clear_key value. For example, include a "Clear Audio Queue" button that increments an internal counter:
if "clear_key" not in st.session_state:
st.session_state["clear_key"] = 0
if st.button("Clear Audio Queue"):
st.session_state["clear_key"] += 1
audio_player(
None,
format="audio/wav",
clear_key=st.session_state["clear_key"],
key="audio_player"
)
Automatic Refresh Sample
The sample app under example/app.py includes a 1-second refresh loop using st.rerun to simulate a dynamic UI while preserving the component's internal state.
dummy.wav
https://www3.jvckenwood.com/pro/soft_dl/pa-d_message/aisatu.html
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file streamlit_audio_queue_player-0.1.1.tar.gz.
File metadata
- Download URL: streamlit_audio_queue_player-0.1.1.tar.gz
- Upload date:
- Size: 388.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
078602f00ce7c6a89608b4ceef6b928355f86aac9bc0b71f5429eaec17af1d69
|
|
| MD5 |
274240b344f0f0c048203e2303e3a6af
|
|
| BLAKE2b-256 |
00315a9143422e4237a7513eaa795cbd4f3abd068fcbc95b812aa2d3ef8208f4
|
File details
Details for the file streamlit_audio_queue_player-0.1.1-py3-none-any.whl.
File metadata
- Download URL: streamlit_audio_queue_player-0.1.1-py3-none-any.whl
- Upload date:
- Size: 391.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6564080315a55b887ce318d4642bd2f22c57ece8cdba9dd4dfaa0e25ad13e8fb
|
|
| MD5 |
c842119d2110562b5a32a73b6aeccf20
|
|
| BLAKE2b-256 |
4d2bad26e8b898a8a808b430aaa54f85e18d64377b72c6e331e5ad548ddb0778
|