AudioRecorder
从麦克风录音到指定的文件路径。适用于 macOS、Linux、Windows、iOS、Android 和 Web。 基于 record Dart/Flutter 包。
备注
在 Linux 上,编码由 fmedia 提供,必须单独安装。
AudioRecorder 控件 是非可视的,应该添加到 page.overlay
列表中。
打包
要构建使用 AudioRecorder
控件的 Flet 应用程序,请在 flet build
命令中添加 --include-packages flet_audio_recorder
,例如:
flet build apk --include-packages flet_audio_recorder
示例
基本示例
- Python
import flet as ft
async def main(page: ft.Page):
page.horizontal_alignment = ft.CrossAxisAlignment.CENTER
page.appbar = ft.AppBar(title=ft.Text("音频录制器"), center_title=True)
path = "test-audio-file.wav"
async def handle_start_recording(e):
print(f"开始录音: {path}")
await audio_rec.start_recording_async(path)
async def handle_stop_recording(e):
output_path = await audio_rec.stop_recording_async()
print(f"停止录音: {output_path}")
if page.web and output_path is not None:
await page.launch_url_async(output_path)
async def handle_list_devices(e):
devices = await audio_rec.get_input_devices_async()
print(devices)
async def handle_has_permission(e):
try:
print(f"有权限: {await audio_rec.has_permission_async()}")
except Exception as e:
print(e)
async def handle_pause(e):
print(f"正在录音: {await audio_rec.is_recording_async()}")
if await audio_rec.is_recording_async():
await audio_rec.pause_recording_async()
async def handle_resume(e):
print(f"已暂停: {await audio_rec.is_paused_async()}")
if await audio_rec.is_paused_async():
await audio_rec.resume_recording_async()
async def handle_audio_encoding_test(e):
for i in list(ft.AudioEncoder):
print(f"{i}: {await audio_rec.is_supported_encoder_async(i)}")
async def handle_state_change(e):
print(f"状态更改: {e.data}")
audio_rec = ft.AudioRecorder(
audio_encoder=ft.AudioEncoder.WAV,
on_state_changed=handle_state_change,
)
page.overlay.append(audio_rec)
await page.update_async()
await page.add_async(
ft.ElevatedButton("开始录音", on_click=handle_start_recording),
ft.ElevatedButton("停止录音", on_click=handle_stop_recording),
ft.ElevatedButton("列出设备", on_click=handle_list_devices),
ft.ElevatedButton("暂停录音", on_click=handle_pause),
ft.ElevatedButton("继续录音", on_click=handle_resume),
ft.ElevatedButton("测试音频编码", on_click=handle_audio_encoding_test),
ft.ElevatedButton("检查权限", on_click=handle_has_permission),
)
ft.app(target=main)