Audio#
Allows playing audio in Flet apps.
Platform Support#
| Platform | Windows | macOS | Linux | iOS | Android | Web | 
|---|---|---|---|---|---|---|
| Supported | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | 
Usage#
To use Audio control add flet-audio package to your project dependencies:
Example#
import flet_audio as fta
import flet as ft
def main(page: ft.Page):
    url = "https://github.com/mdn/webaudio-examples/blob/main/audio-analyser/viper.mp3?raw=true"
    async def play():
        await audio.play()
    async def pause():
        await audio.pause()
    async def resume():
        await audio.resume()
    async def release():
        await audio.release()
    def set_volume(value: float):
        audio.volume += value
    def set_balance(value: float):
        audio.balance += value
    async def seek_2s():
        await audio.seek(ft.Duration(seconds=2))
    async def get_duration():
        duration = await audio.get_duration()
        print("Duration:", duration)
    async def on_get_current_position():
        position = await audio.get_current_position()
        print("Current position:", position)
    audio = fta.Audio(
        src=url,
        autoplay=False,
        volume=1,
        balance=0,
        on_loaded=lambda _: print("Loaded"),
        on_duration_change=lambda e: print("Duration changed:", e.duration),
        on_position_change=lambda e: print("Position changed:", e.position),
        on_state_change=lambda e: print("State changed:", e.state),
        on_seek_complete=lambda _: print("Seek complete"),
    )
    page.add(
        ft.Button("Play", on_click=play),
        ft.Button("Pause", on_click=pause),
        ft.Button("Resume", on_click=resume),
        ft.Button("Release", on_click=release),
        ft.Button("Seek 2s", on_click=seek_2s),
        ft.Row(
            controls=[
                ft.Button("Volume down", on_click=lambda _: set_volume(-0.1)),
                ft.Button("Volume up", on_click=lambda _: set_volume(0.1)),
            ]
        ),
        ft.Row(
            controls=[
                ft.Button("Balance left", on_click=lambda _: set_balance(-0.1)),
                ft.Button("Balance right", on_click=lambda _: set_balance(0.1)),
            ]
        ),
        ft.Button("Get duration", on_click=get_duration),
        ft.Button("Get current position", on_click=on_get_current_position),
    )
ft.run(main)
Linux/WSL (Windows Subsystem for Linux)
To play audio on Linux/WSL you need to install GStreamer library.
If you receive error while loading shared libraries: libgstapp-1.0.so.0,
it means GStreamer is not installed in your WSL environment.
To install it, run the following command:
Description#
        Inherits: Service
A control to simultaneously play multiple audio sources.
Properties
- 
          autoplay(bool) –Starts playing audio as soon as audio control is added to a page. 
- 
          balance(Number) –Defines the stereo balance. 
- 
          playback_rate(Number) –Defines the playback rate. 
- 
          release_mode(ReleaseMode) –Defines the release mode. 
- 
          src(str | None) –The audio source. 
- 
          src_base64(str | None) –Defines the contents of audio file encoded in base-64 format. 
- 
          volume(Number) –Sets the volume (amplitude). 
Events
- 
          on_duration_change(EventHandler[AudioDurationChangeEvent] | None) –Fires as soon as audio duration is available 
- 
          on_loaded(ControlEventHandler[Audio] | None) –Fires when an audio is loaded/buffered. 
- 
          on_position_change(EventHandler[AudioPositionChangeEvent] | None) –Fires when audio position is changed. 
- 
          on_seek_complete(ControlEventHandler[Audio] | None) –Fires on seek completions. 
- 
          on_state_change(EventHandler[AudioStateChangeEvent] | None) –Fires when audio player state changes. 
Methods
- 
            get_current_position–Get the current position of the audio playback. 
- 
            get_duration–Get audio duration of the audio playback. 
- 
            pause–Pauses the audio that is currently playing. 
- 
            play–Starts playing audio from the specified position.
- 
            release–Releases the resources associated with this media player. 
- 
            resume–Resumes the audio that has been paused or stopped. 
- 
            seek–Moves the cursor to the desired position. 
Properties#
class-attribute
      instance-attribute
  
#
autoplay: bool = False
Starts playing audio as soon as audio control is added to a page.
Note
Autoplay works in desktop, mobile apps and Safari browser, but doesn't work in Chrome/Edge.
class-attribute
      instance-attribute
  
#
balance: Number = 0.0
Defines the stereo balance.
- -1- The left channel is at full volume; the right channel is silent.
- 1- The right channel is at full volume; the left channel is silent.
- 0- Both channels are at the same volume.
class-attribute
      instance-attribute
  
#
playback_rate: Number = 1.0
Defines the playback rate.
Should ideally be set when creating the constructor.
Note
- iOS and macOS have limits between 0.5xand2x.
- Android SDK version should be 23 or higher.
class-attribute
      instance-attribute
  
#
release_mode: ReleaseMode = RELEASE
Defines the release mode.
class-attribute
      instance-attribute
  
#
src: str | None = None
The audio source. Can be a URL or a local asset file.
Note
- At least one of srcorsrc_base64must be provided, withsrc_base64having priority if both are provided.
- Here is a list of supported audio formats.
Raises:
- 
              ValueError–If both srcandsrc_base64areNone.
class-attribute
      instance-attribute
  
#
src_base64: str | None = None
Defines the contents of audio file encoded in base-64 format.
Note
Raises:
- 
              ValueError–If both src_base64andsrcareNone.
class-attribute
      instance-attribute
  
#
volume: Number = 1.0
Sets the volume (amplitude).
It's value ranges between 0.0 (mute) and 1.0 (maximum volume).
Intermediate values are linearly interpolated.
Events#
class-attribute
      instance-attribute
  
#
on_duration_change: (
    EventHandler[AudioDurationChangeEvent] | None
) = None
Fires as soon as audio duration is available (it might take a while to download or buffer it).
class-attribute
      instance-attribute
  
#
on_loaded: ControlEventHandler[Audio] | None = None
Fires when an audio is loaded/buffered.
class-attribute
      instance-attribute
  
#
on_position_change: (
    EventHandler[AudioPositionChangeEvent] | None
) = None
Fires when audio position is changed. Will continuously update the position of the playback every 1 second if the status is playing.
Can be used for a progress bar.
class-attribute
      instance-attribute
  
#
on_seek_complete: ControlEventHandler[Audio] | None = None
Fires on seek completions. An event is going to be sent as soon as the audio seek is finished.
class-attribute
      instance-attribute
  
#
on_state_change: (
    EventHandler[AudioStateChangeEvent] | None
) = None
Fires when audio player state changes.
Methods#
async
  
#
get_current_position() -> Duration | None
Get the current position of the audio playback.
Returns:
- 
              Duration | None–The current position of the audio playback. 
async
  
#
get_duration() -> Duration | None
Get audio duration of the audio playback.
It will be available as soon as the audio duration is available (it might take a while to download or buffer it if file is not local).
Returns:
- 
              Duration | None–The duration of audio playback. 
async
  
#
    Pauses the audio that is currently playing.
If you call resume() later,
the audio will resume from the point that it has been paused.
async
  
#
play(position: DurationValue = 0)
Starts playing audio from the specified position.
Parameters:
- 
            position(DurationValue, default:0) –The position to start playback from. 
async
  
#
    Releases the resources associated with this media player.
These are going to be fetched or buffered again as soon as
you change the source or call resume().
async
  
#
seek(position: DurationValue)
Moves the cursor to the desired position.
Parameters:
- 
            position(DurationValue) –The position to seek/move to.