Audio Recorder
Allows recording audio in Flet apps.
It is based on the record Flutter package.
Platform Support
| Platform | Windows | macOS | Linux | iOS | Android | Web |
|---|---|---|---|---|---|---|
| Supported | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
Usage
To use AudioRecorder service add flet-audio-recorder package to your project dependencies:
- uv
- pip
uv add flet-audio-recorder
pip install flet-audio-recorder
Requirements
The below sections show the required configurations for each platform.
Android
Configuration to be made to access the microphone:
android.permission.RECORD_AUDIO: Allows audio recording.android.permission.WRITE_EXTERNAL_STORAGE(optional): Allows saving your recordings in public folders.android.permission.MODIFY_AUDIO_SETTINGS(optional): Allows using bluetooth telephony device like headset/earbuds.
- flet build
- pyproject.toml
flet build apk \
--android-permissions android.permission.RECORD_AUDIO=true \
--android-permissions android.permission.WRITE_EXTERNAL_STORAGE=true \
--android-permissions android.permission.MODIFY_AUDIO_SETTINGS=true
[tool.flet.android.permission]
"android.permission.RECORD_AUDIO" = true
"android.permission.WRITE_EXTERNAL_STORAGE" = true
"android.permission.MODIFY_AUDIO_SETTINGS" = true
See also:
iOS
Configuration to be made to access the microphone:
NSMicrophoneUsageDescription: Required for recording audio.
- flet build
- pyproject.toml
flet build ipa \
--info-plist NSMicrophoneUsageDescription="Some message to describe why you need this permission..."
[tool.flet.ios.info]
NSMicrophoneUsageDescription = "Some message to describe why you need this permission..."
See also:
macOS
Configuration to be made to access the microphone:
NSMicrophoneUsageDescription: Allows recording audio.com.apple.security.device.audio-input: Allows recording audio using the built-in microphone and accessing audio input using Core Audio.
- flet build
- pyproject.toml
flet build macos \
--info-plist NSMicrophoneUsageDescription="Some message to describe why you need this permission..." \
--macos-entitlements com.apple.security.device.audio-input=true
[tool.flet.macos.info]
NSMicrophoneUsageDescription = "Some message to describe why you need this permission..."
[tool.flet.macos.entitlement]
"com.apple.security.device.audio-input" = true
See also:
Linux
The following dependencies are required (and widely available on your system):
parecord: Used for audio input.pactl: Used for utility methods like getting available devices.ffmpeg: Used for encoding and output.
On Ubuntu 24.04.3 LTS, you can install them using:
sudo apt install pulseaudio-utils ffmpeg
Cross-platform
Additionally/alternatively, you can make use of our predefined cross-platform microphone
permission bundle:
- flet build
- pyproject.toml
flet build <target_platform> --permissions microphone
[tool.flet]
permissions = ["microphone"]
Example
import flet as ft
import flet_audio_recorder as far
def main(page: ft.Page):
page.horizontal_alignment = ft.CrossAxisAlignment.CENTER
page.appbar = ft.AppBar(title=ft.Text("Audio Recorder"), center_title=True)
path = "test-audio-file.wav"
def show_snackbar(message):
page.show_dialog(ft.SnackBar(ft.Text(message)))
async def handle_recording_start(e: ft.Event[ft.Button]):
show_snackbar("Starting recording...")
await recorder.start_recording(output_path=path)
async def handle_recording_stop(e: ft.Event[ft.Button]):
output_path = await recorder.stop_recording()
show_snackbar(f"Stopped recording. Output Path: {output_path}")
if page.web and output_path is not None:
await page.launch_url(output_path)
async def handle_list_devices(e: ft.Event[ft.Button]):
o = await recorder.get_input_devices()
show_snackbar(f"Input Devices: {', '.join([f'{d.id}:{d.label}' for d in o])}")
async def handle_has_permission(e: ft.Event[ft.Button]):
try:
status = await recorder.has_permission()
show_snackbar(f"Audio Recording Permission status: {status}")
except Exception as e:
show_snackbar(f"Error checking permission: {e}")
async def handle_pause(e: ft.Event[ft.Button]):
print(f"isRecording: {await recorder.is_recording()}")
if await recorder.is_recording():
await recorder.pause_recording()
async def handle_resume(e: ft.Event[ft.Button]):
print(f"isPaused: {await recorder.is_paused()}")
if await recorder.is_paused():
await recorder.resume_recording()
async def handle_audio_encoder_test(e: ft.Event[ft.Button]):
print(await recorder.is_supported_encoder(far.AudioEncoder.WAV))
recorder = far.AudioRecorder(
configuration=far.AudioRecorderConfiguration(encoder=far.AudioEncoder.WAV),
on_state_change=lambda e: print(f"State Changed: {e.data}"),
)
page.add(
ft.SafeArea(
content=ft.Column(
controls=[
ft.Button(
content="Start Audio Recorder", on_click=handle_recording_start
),
ft.Button(
content="Stop Audio Recorder", on_click=handle_recording_stop
),
ft.Button(content="List Devices", on_click=handle_list_devices),
ft.Button(content="Pause Recording", on_click=handle_pause),
ft.Button(content="Resume Recording", on_click=handle_resume),
ft.Button(
content="WAV Encoder Support",
on_click=handle_audio_encoder_test,
),
ft.Button(
content="Get Audio Recording Permission Status",
on_click=handle_has_permission,
),
],
),
)
)
if __name__ == "__main__":
ft.run(main)
Description
A control that allows you to record audio from your device.
This control can record audio using different audio encoders and also allows configuration of various audio recording parameters such as noise suppression, echo cancellation, and more.
Inherits: Service
Properties
configuration- The default configuration of the audio recorder.
Events
on_state_change- Event handler that is called when the state of the audio recorder changes.
Methods
cancel_recording- Cancels the current audio recording.get_input_devices- Retrieves the available input devices for recording.has_permission- Checks if the app has permission to record audio.is_paused- Checks whether the audio recorder is currently paused.is_recording- Checks whether the audio recorder is currently recording.is_supported_encoder- Checks if the given audio encoder is supported by the recorder.pause_recording- Pauses the ongoing audio recording.resume_recording- Resumes a paused audio recording.start_recording- Starts recording audio and saves it to the specified output path.stop_recording- Stops the audio recording and optionally returns the path to the saved file.
Properties
configurationclass-attributeinstance-attribute
configuration: AudioRecorderConfiguration = field(default_factory=(lambda: AudioRecorderConfiguration()))The default configuration of the audio recorder.
Events
on_state_changeclass-attributeinstance-attribute
on_state_change: Optional[EventHandler[AudioRecorderStateChangeEvent]] = NoneEvent handler that is called when the state of the audio recorder changes.
Methods
get_input_devicesasync
get_input_devices()Retrieves the available input devices for recording.
Returns:
- list[InputDevice] - A list of available input devices.
has_permissionasync
has_permission()Checks if the app has permission to record audio.
Returns:
- bool -
Trueif the app has permission,Falseotherwise.
is_pausedasync
is_paused()Checks whether the audio recorder is currently paused.
Returns:
- bool -
Trueif the recorder is paused,Falseotherwise.
is_recordingasync
is_recording()Checks whether the audio recorder is currently recording.
Returns:
- bool -
Trueif the recorder is currently recording,Falseotherwise.
is_supported_encoderasync
is_supported_encoder(encoder: AudioEncoder)Checks if the given audio encoder is supported by the recorder.
Parameters:
- encoder (AudioEncoder) - The audio encoder to check.
Returns:
- bool -
Trueif the encoder is supported,Falseotherwise.
start_recordingasync
start_recording(output_path: Optional[str] = None, configuration: Optional[AudioRecorderConfiguration] = None)Starts recording audio and saves it to the specified output path.
If not on the web, the output_path parameter must be provided.
Parameters:
- output_path (Optional[str], default:
None) - The file path where the audio will be saved. It must be specified if not on web. - configuration (Optional[AudioRecorderConfiguration], default:
None) - The configuration for the audio recorder. IfNone, theAudioRecorder.configurationwill be used.
Returns:
- bool -
Trueif recording was successfully started,Falseotherwise.
Raises:
- ValueError - If
output_pathis not provided on platforms other than web.
stop_recordingasync
stop_recording()Stops the audio recording and optionally returns the path to the saved file.
Returns:
- Optional[str] - The file path where the audio was saved or
Noneif not applicable.