|
| 1 | +import flet as ft |
| 2 | + |
| 3 | +import flet_audio_recorder as far |
| 4 | + |
| 5 | + |
| 6 | +def main(page: ft.Page): |
| 7 | + page.horizontal_alignment = ft.CrossAxisAlignment.CENTER |
| 8 | + page.appbar = ft.AppBar(title=ft.Text("Audio Recorder"), center_title=True) |
| 9 | + |
| 10 | + path = "test-audio-file.wav" |
| 11 | + |
| 12 | + def show_snackbar(message): |
| 13 | + page.show_dialog(ft.SnackBar(ft.Text(message))) |
| 14 | + |
| 15 | + async def handle_recording_start(e: ft.Event[ft.Button]): |
| 16 | + show_snackbar("Starting recording...") |
| 17 | + await recorder.start_recording(output_path=path) |
| 18 | + |
| 19 | + async def handle_recording_stop(e: ft.Event[ft.Button]): |
| 20 | + output_path = await recorder.stop_recording() |
| 21 | + show_snackbar(f"Stopped recording. Output Path: {output_path}") |
| 22 | + if page.web and output_path is not None: |
| 23 | + await page.launch_url(output_path) |
| 24 | + |
| 25 | + async def handle_list_devices(e: ft.Event[ft.Button]): |
| 26 | + o = await recorder.get_input_devices() |
| 27 | + show_snackbar(f"Input Devices: {', '.join([d.label for d in o])}") |
| 28 | + |
| 29 | + async def handle_has_permission(e: ft.Event[ft.Button]): |
| 30 | + try: |
| 31 | + status = await recorder.has_permission() |
| 32 | + show_snackbar(f"Audio Recording Permission status: {status}") |
| 33 | + except Exception as e: |
| 34 | + show_snackbar(f"Error checking permission: {e}") |
| 35 | + |
| 36 | + async def handle_pause(e: ft.Event[ft.Button]): |
| 37 | + print(f"isRecording: {await recorder.is_recording()}") |
| 38 | + if await recorder.is_recording(): |
| 39 | + await recorder.pause_recording() |
| 40 | + |
| 41 | + async def handle_resume(e: ft.Event[ft.Button]): |
| 42 | + print(f"isPaused: {await recorder.is_paused()}") |
| 43 | + if await recorder.is_paused(): |
| 44 | + await recorder.resume_recording() |
| 45 | + |
| 46 | + async def handle_audio_encoder_test(e: ft.Event[ft.Button]): |
| 47 | + print(await recorder.is_supported_encoder(far.AudioEncoder.WAV)) |
| 48 | + |
| 49 | + recorder = far.AudioRecorder( |
| 50 | + configuration=far.AudioRecorderConfiguration(encoder=far.AudioEncoder.WAV), |
| 51 | + on_state_change=lambda e: print(f"State Changed: {e.data}"), |
| 52 | + ) |
| 53 | + page.services.append(recorder) |
| 54 | + |
| 55 | + page.add( |
| 56 | + ft.Button(content="Start Audio Recorder", on_click=handle_recording_start), |
| 57 | + ft.Button(content="Stop Audio Recorder", on_click=handle_recording_stop), |
| 58 | + ft.Button(content="List Devices", on_click=handle_list_devices), |
| 59 | + ft.Button(content="Pause Recording", on_click=handle_pause), |
| 60 | + ft.Button(content="Resume Recording", on_click=handle_resume), |
| 61 | + ft.Button(content="WAV Encoder Support", on_click=handle_audio_encoder_test), |
| 62 | + ft.Button( |
| 63 | + content="Get Audio Recording Permission Status", |
| 64 | + on_click=handle_has_permission, |
| 65 | + ), |
| 66 | + ) |
| 67 | + |
| 68 | + |
| 69 | +ft.run(main) |
0 commit comments