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)
Windows Subsystem for Linux (WSL)
On 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#
autoplay
#
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.
balance
#
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.
playback_rate
#
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.5x
and2x
. - Android SDK version should be 23 or higher.
src
#
src: str | None = None
The audio source. Can be a URL or a local asset file.
Note
- At least one of
src
orsrc_base64
must be provided, withsrc_base64
having priority if both are provided. - Here is a list of supported audio formats.
Raises:
-
AssertionError
–If both
src
andsrc_base64
areNone
.
src_base64
#
src_base64: str | None = None
Defines the contents of audio file encoded in base-64 format.
Note
Raises:
-
AssertionError
–If both
src
andsrc_base64
areNone
.
volume
#
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#
on_duration_change
#
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).
on_loaded
#
on_loaded: ControlEventHandler[Audio] | None = None
Fires when an audio is loaded/buffered.
on_position_change
#
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.
on_seek_complete
#
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.
on_state_change
#
on_state_change: (
EventHandler[AudioStateChangeEvent] | None
) = None
Fires when audio player state changes.
Methods#
get_current_position
#
get_current_position() -> Duration | None
Get the current position of the audio playback.
Returns:
-
Duration | None
–The current position of the audio playback.
get_duration
#
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.
pause
#
Pauses the audio that is currently playing.
If you call resume()
later,
the audio will resume from the point that it has been paused.
play
#
play(position: DurationValue = 0)
Starts playing audio from the specified position
.
Parameters:
-
position
(DurationValue
, default:0
) –The position to start playback from.
release
#
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()
.
seek
#
seek(position: DurationValue)
Moves the cursor to the desired position.
Parameters:
-
position
(DurationValue
) –The position to seek/move to.