website/docs/services/audiorecorder/index.md
import TabItem from '@theme/TabItem'; import Tabs from '@theme/Tabs'; import {ClassAll, CodeExample} from '@site/src/components/crocodocs';
Allows recording audio in Flet apps.
It is based on the record Flutter package.
| Platform | Windows | macOS | Linux | iOS | Android | Web |
|---|---|---|---|---|---|---|
| Supported | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
To use AudioRecorder service add flet-audio-recorder package to your project dependencies:
requirements.txt or pyproject.toml.
</TabItem>
The below sections show the required configurations for each platform.
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.Configuration to be made to access the microphone:
NSMicrophoneUsageDescription: Required for recording audio.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.[tool.flet.macos.entitlement] "com.apple.security.device.audio-input" = true
</TabItem>
</Tabs>
See also:
- [setting macOS permissions](../../publish/macos.md#permissions)
- [setting macOS entitlements](../../publish/macos.md#entitlements)
### 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`](https://ffmpeg.org): Used for encoding and output.
On Ubuntu 24.04.3 LTS, you can install them using:
```bash
sudo apt install pulseaudio-utils ffmpeg
Additionally/alternatively, you can make use of our predefined cross-platform microphone
permission bundle:
<CodeExample path={frontMatter.examples + '/basic/main.py'} language="python" />
On web, [AudioRecorder.stop_recording()][flet_audio_recorder.AudioRecorder.stop_recording]
returns a browser-local Blob URL. Use streaming when your app needs access to the recorded bytes.
Set [AudioRecorderConfiguration.encoder][flet_audio_recorder.AudioRecorderConfiguration.encoder]
to [AudioEncoder.PCM16BITS][flet_audio_recorder.AudioEncoder.PCM16BITS]
and handle [AudioRecorder.on_stream][flet_audio_recorder.AudioRecorder.on_stream]
to receive [AudioRecorderStreamEvent.chunk][flet_audio_recorder.AudioRecorderStreamEvent.chunk]
bytes in Python.
[AudioEncoder.PCM16BITS][flet_audio_recorder.AudioEncoder.PCM16BITS]
encoded streams are supported on all platforms. Stream chunks are raw PCM16 bytes and are not directly
playable as an audio file. Wrap the bytes in a container such as WAV in Python when
the destination needs a directly playable recording.
The example below collects the streamed chunks, wraps the PCM16 bytes in a WAV container,
and passes the resulting bytes to [FilePicker.save_file()][flet.FilePicker.save_file]
so users can save or download the recording.
<CodeExample path={frontMatter.examples + '/stream_and_download/main.py'} language="python" />
Pass [AudioRecorderUploadSettings][flet_audio_recorder.AudioRecorderUploadSettings] to
[AudioRecorder.start_recording()][flet_audio_recorder.AudioRecorder.start_recording]
to upload [AudioEncoder.PCM16BITS][flet_audio_recorder.AudioEncoder.PCM16BITS]
recording bytes directly while recording.
The uploaded file contains raw PCM16 bytes, so a .pcm extension is intentional.
See the stream chunks and save/download example
for inspiration, if you need to create a playable WAV file.
:::note
Built-in upload URLs from [Page.get_upload_url()][flet.Page.get_upload_url]
require upload storage and a signing key. Run with upload_dir and set
FLET_SECRET_KEY.
:::
<CodeExample path={frontMatter.examples + '/upload/main.py'} language="python" />