Back to Flet

AudioRecorder

website/docs/services/audiorecorder/index.md

0.85.0.dev37.4 KB
Original Source

import TabItem from '@theme/TabItem'; import Tabs from '@theme/Tabs'; import {ClassAll, CodeExample} from '@site/src/components/crocodocs';

Audio Recorder

Allows recording audio in Flet apps.

It is based on the record Flutter package.

Platform Support

PlatformWindowsmacOSLinuxiOSAndroidWeb
Supported

Usage

To use AudioRecorder service add flet-audio-recorder package to your project dependencies:

<Tabs groupId="uv--pip"> <TabItem value="uv" label="uv"> ```bash uv add flet-audio-recorder ``` </TabItem> <TabItem value="pip" label="pip"> ```bash pip install flet-audio-recorder # (1)! ```
  1. After this, you will have to manually add this package to your requirements.txt or pyproject.toml. </TabItem>
</Tabs> ## Requirements

The below sections show the required configurations for each platform.

Android

Configuration to be made to access the microphone:

<Tabs groupId="flet-build--pyproject-toml"> <TabItem value="flet-build" label="flet build"> ```bash 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 ``` </TabItem> <TabItem value="pyproject-toml" label="pyproject.toml"> ```toml [tool.flet.android.permission] "android.permission.RECORD_AUDIO" = true "android.permission.WRITE_EXTERNAL_STORAGE" = true "android.permission.MODIFY_AUDIO_SETTINGS" = true ``` </TabItem> </Tabs> See also:

iOS

Configuration to be made to access the microphone:

<Tabs groupId="flet-build--pyproject-toml"> <TabItem value="flet-build" label="flet build"> ```bash flet build ipa \ --info-plist NSMicrophoneUsageDescription="Some message to describe why you need this permission..." ``` </TabItem> <TabItem value="pyproject-toml" label="pyproject.toml"> ```toml [tool.flet.ios.info] NSMicrophoneUsageDescription = "Some message to describe why you need this permission..." ``` </TabItem> </Tabs> See also:

macOS

Configuration to be made to access the microphone:

<Tabs groupId="flet-build--pyproject-toml"> <TabItem value="flet-build" label="flet build"> ```bash flet build macos \ --info-plist NSMicrophoneUsageDescription="Some message to describe why you need this permission..." \ --macos-entitlements com.apple.security.device.audio-input=true ``` </TabItem> <TabItem value="pyproject-toml" label="pyproject.toml"> ```toml [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

</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

Cross-platform

Additionally/alternatively, you can make use of our predefined cross-platform microphone permission bundle:

<Tabs groupId="flet-build--pyproject-toml"> <TabItem value="flet-build" label="flet build"> ```bash flet build <target_platform> --permissions microphone ``` </TabItem> <TabItem value="pyproject-toml" label="pyproject.toml"> ```toml [tool.flet] permissions = ["microphone"] ``` </TabItem> </Tabs>

Examples

Basic recording

<CodeExample path={frontMatter.examples + '/basic/main.py'} language="python" />

Stream chunks and save/download {#stream-chunks-and-save-download}

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" />

Streaming upload

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" />

Description

<ClassAll name={frontMatter.class_name} />