2 3 Displaying Audio Explained
Key Concepts
- st.audio: A Streamlit function to display and play audio files.
- Audio File Formats: Supported formats like MP3, WAV, etc.
- Audio Playback: Controlling the playback of audio files.
st.audio
st.audio
is a Streamlit function that allows you to embed and play audio files directly within your application. This function is particularly useful for applications that require audio feedback, such as music players, podcasts, or voice recordings.
Audio File Formats
The st.audio
function supports various audio file formats, including MP3, WAV, FLAC, and OGG. When using this function, ensure that the audio file is in a supported format to ensure compatibility and proper playback.
Audio Playback
Once an audio file is embedded using st.audio
, users can control the playback directly from the Streamlit interface. This includes features like play, pause, and volume control. The audio player is automatically rendered, providing a seamless user experience.
Examples
Here are some examples to illustrate the use of st.audio
:
import streamlit as st st.title("Audio Player Example") # Basic audio player audio_file = open("sample.mp3", "rb") audio_bytes = audio_file.read() st.audio(audio_bytes, format="audio/mp3") # Audio player with a caption st.audio(audio_bytes, format="audio/mp3", start_time=0) st.write("Listen to this sample audio clip.")
Analogies
Think of st.audio
as a digital audio player embedded within your Streamlit application. Just like a physical audio player, users can play, pause, and control the volume of the audio. The supported file formats ensure that the player can handle a variety of audio types, similar to how a versatile audio player can play different types of CDs or files.
By mastering st.audio
, you can enhance your Streamlit applications by providing audio capabilities, making your apps more interactive and engaging.