Display an audio player.

Function signature[source]

st.audio(data, format="audio/wav", start_time=0, *, sample_rate=None, end_time=None, loop=False)

Parameters

data (str, bytes, BytesIO, numpy.ndarray, or file)

Raw audio data, filename, or a URL pointing to the file to load. Raw data formats must include all necessary file headers to match the file format specified via format. If data is a numpy array, it must either be a 1D array of the waveform or a 2D array of shape (num_channels, num_samples) with waveforms for all channels. See the default channel order at http://msdn.microsoft.com/en-us/library/windows/hardware/dn653308(v=vs.85).aspx

format (str)

The mime type for the audio file. Defaults to "audio/wav". See https://tools.ietf.org/html/rfc4281 for more info.

start_time (int, float, timedelta, str, or None)

The time from which the element should start playing. This can be one of the following:

  • None (default): The element plays from the beginning.
  • An``int`` or float specifying the time in seconds. float values are rounded down to whole seconds.
  • A string specifying the time in a format supported by Pandas' Timedelta constructor, e.g. "2 minute", "20s", or "1m14s".
  • A timedelta object from Python's built-in datetime library, e.g. timedelta(seconds=70).

sample_rate (int or None)

The sample rate of the audio data in samples per second. Only required if data is a numpy array.

end_time (int, float, timedelta, str, or None)

The time at which the element should stop playing. This can be one of the following:

  • None (default): The element plays through to the end.
  • An int or float specifying the time in seconds. float values are rounded down to whole seconds.
  • A string specifying the time in a format supported by Pandas' Timedelta constructor, e.g. "2 minute", "20s", or "1m14s".
  • A timedelta object from Python's built-in datetime library, e.g. timedelta(seconds=70).

loop (bool)

Whether the audio should loop playback.

Examples

To display an audio player for a local file, specify the file's string path and format.

import streamlit as st

st.audio("cat-purr.mp3", format="audio/mpeg", loop=True)

You can also pass bytes or numpy.ndarray objects to st.audio.

import streamlit as st
import numpy as np

audio_file = open("myaudio.ogg", "rb")
audio_bytes = audio_file.read()

st.audio(audio_bytes, format="audio/ogg")

sample_rate = 44100  # 44100 samples per second
seconds = 2  # Note duration of 2 seconds
frequency_la = 440  # Our played note will be 440 Hz
# Generate array with seconds*sample_rate steps, ranging between 0 and seconds
t = np.linspace(0, seconds, seconds * sample_rate, False)
# Generate a 440 Hz sine wave
note_la = np.sin(frequency_la * t * 2 * np.pi)

st.audio(note_la, sample_rate=sample_rate)
forum

Still have questions?

Our forums are full of helpful information and Streamlit experts.