Danisz commited on
Commit
10c8897
·
1 Parent(s): f1c6373

Let's test audio without async

Browse files
Files changed (2) hide show
  1. .gitignore +1 -0
  2. app.py +54 -133
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ .venv
app.py CHANGED
@@ -1,172 +1,93 @@
1
  import flet as ft
 
2
  import os
3
- from pathlib import Path
4
- import sys
5
  import base64
6
- import mimetypes
7
- import tempfile
8
 
9
  def main(page: ft.Page):
10
- folder_path = "/tmp"
11
- can_read = os.access(folder_path, os.R_OK)
12
- can_write = os.access(folder_path, os.W_OK)
13
- can_execute = os.access(folder_path, os.X_OK)
14
-
15
- print(f"Read permission: {can_read}")
16
- print(f"Write permission: {can_write}")
17
- print(f"Execute permission: {can_execute}")
18
- page.title = "Flet File System Explorer"
19
-
20
- current_path = os.getcwd()
21
- show_hidden = False
22
-
23
- # Creating a TextField for file content
24
- txt_content = ft.TextField(value="", multiline=True, width=300, visible=True)
25
-
26
- # Audio components
27
- audio_player = ft.Audio()
28
- audio_text = ft.Text(visible=False)
29
- audio_button = ft.ElevatedButton("Play", visible=False, on_click=lambda _: audio_player.play())
30
 
31
  temp_recording_file = None
32
 
33
  def start_recording(e):
34
  record_button.disabled = True
35
  stop_button.disabled = False
 
36
  page.update()
37
 
38
  nonlocal temp_recording_file
39
  temp_recording_file = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
40
  print(f"Starting recording to {temp_recording_file.name}")
41
- audio_recorder.start_recording(temp_recording_file.name)
42
  page.update()
43
 
44
  def stop_recording(e):
45
  record_button.disabled = False
46
  stop_button.disabled = True
 
 
47
  page.update()
48
- audio_recorder.stop_recording()
 
 
 
 
 
 
 
 
 
 
 
 
49
  page.update()
50
 
 
 
 
 
51
  def update_recording_status(status):
52
  recording_status.value = f"Recording status: {status}"
53
- if status == "stopped":
54
- save_recording()
55
  page.update()
56
 
57
- def save_recording():
58
- nonlocal temp_recording_file
59
- if temp_recording_file:
60
- new_file_path = os.path.join(current_path, f"recording_{len(os.listdir(current_path))}.wav")
61
- os.rename(temp_recording_file.name, new_file_path)
62
- temp_recording_file = None
63
- update_directory()
64
-
65
- # New audio recording components
66
- audio_recorder = ft.AudioRecorder(
67
- on_state_changed=lambda e: update_recording_status(e.data)
68
- )
69
- recording_status = ft.Text()
70
  record_button = ft.ElevatedButton("Start Recording", on_click=start_recording)
71
  stop_button = ft.ElevatedButton("Stop Recording", on_click=stop_recording, disabled=True)
 
72
 
73
- # Creating a ListView for directory contents
74
- lv_directory = ft.ListView(expand=1, spacing=10)
75
-
76
- # Function to update directory contents
77
- def update_directory():
78
- lv_directory.controls.clear()
79
- for item in os.listdir(current_path):
80
- if not show_hidden and item.startswith('.'):
81
- continue
82
- item_path = os.path.join(current_path, item)
83
- icon = ft.icons.FOLDER if os.path.isdir(item_path) else ft.icons.INSERT_DRIVE_FILE
84
- lv_directory.controls.append(
85
- ft.ListTile(
86
- leading=ft.Icon(icon),
87
- title=ft.Text(item),
88
- on_click=lambda _, i=item: navigate_or_open(i)
89
- )
90
- )
91
- txt_path.value = current_path
92
- page.update()
93
 
94
- # Function to navigate directories or open files
95
- def navigate_or_open(item):
96
- nonlocal current_path
97
- item_path = os.path.join(current_path, item)
98
- if os.path.isdir(item_path):
99
- current_path = item_path
100
- update_directory()
101
- else:
102
- if item_path.lower().endswith(('.wav', '.aac')):
103
- display_audio_player(item_path)
104
- else:
105
- display_file_content(item_path)
106
- page.update()
107
 
108
- # Function to display audio player
109
- def display_audio_player(audio_path):
110
- txt_content.visible = False
111
-
112
- mime_type = 'audio/wav'
113
-
114
- with open(audio_path, "rb") as file:
115
- audio_bytes = file.read()
116
- audio_base64 = base64.b64encode(audio_bytes).decode('utf-8')
117
- url = f"data:{mime_type};base64,{audio_base64}"
118
-
119
- # Create new Audio control
120
- audio_player = ft.Audio(src=url, autoplay=False)
121
- page.overlay.append(audio_player)
122
-
123
- audio_text.value = f"Audio file: {os.path.basename(audio_path)}"
124
- audio_text.visible = True
125
-
126
- audio_button.text = "Play"
127
- audio_button.on_click = lambda _: audio_player.play()
128
- audio_button.visible = True
129
-
130
- page.update()
131
 
132
- # Function to display file content
133
- def display_file_content(file_path):
134
- txt_content.visible = True
135
- audio_text.visible = False
136
- audio_button.visible = False
137
- with open(file_path, 'r') as file:
138
- txt_content.value = file.read()
139
-
140
- # Button click event to go up one directory
141
- def go_up(e):
142
- nonlocal current_path
143
- current_path = os.path.dirname(current_path)
144
- update_directory()
145
-
146
- # Checkbox change event to toggle hidden files
147
- def toggle_hidden(e):
148
- nonlocal show_hidden
149
- show_hidden = e.control.value
150
- update_directory()
151
-
152
- # TextField to show current path
153
- txt_path = ft.TextField(value=current_path, read_only=True, width=300)
154
-
155
- # Checkbox to toggle hidden files
156
- chk_hidden = ft.Checkbox(label="Show hidden files", value=False, on_change=toggle_hidden)
157
-
158
- # Adding controls to the page
159
  page.add(
160
- ft.Row([txt_path, ft.ElevatedButton("Up", on_click=go_up)]),
161
- chk_hidden,
162
- ft.Row([
163
- ft.Column([lv_directory], expand=1),
164
- ft.Column([txt_content, audio_text, audio_button], expand=1)
165
- ], expand=True),
166
- ft.Row([record_button, stop_button, recording_status]), # New row for recording controls
167
  )
168
 
169
- page.overlay.append(audio_recorder)
170
- update_directory()
171
 
172
  app = ft.app(target=main, export_asgi_app=True)
 
1
  import flet as ft
2
+ import tempfile
3
  import os
 
 
4
  import base64
 
 
5
 
6
  def main(page: ft.Page):
7
+ page.title = "Simple Audio Recorder"
8
+
9
+ audio_recorder = ft.AudioRecorder(
10
+ audio_encoder=ft.AudioEncoder.WAV,
11
+ on_state_changed=lambda e: update_recording_status(e.data)
12
+ )
13
+ recording_status = ft.Text()
14
+ audio_player = None # Initialize audio_player as None
15
+
16
+ page.overlay.append(audio_recorder) # Only add audio_recorder to overlay initially
17
+
18
+ page.update()
 
 
 
 
 
 
 
 
19
 
20
  temp_recording_file = None
21
 
22
  def start_recording(e):
23
  record_button.disabled = True
24
  stop_button.disabled = False
25
+ play_button.disabled = True
26
  page.update()
27
 
28
  nonlocal temp_recording_file
29
  temp_recording_file = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
30
  print(f"Starting recording to {temp_recording_file.name}")
31
+ audio_recorder.start_recording(temp_recording_file.name, wait_timeout=2)
32
  page.update()
33
 
34
  def stop_recording(e):
35
  record_button.disabled = False
36
  stop_button.disabled = True
37
+ play_button.disabled = False
38
+
39
  page.update()
40
+ nonlocal audio_player
41
+ result_uri = audio_recorder.stop_recording()
42
+ print("result_uri")
43
+ print(result_uri)
44
+
45
+ # Create audio_player after recording is complete
46
+ if temp_recording_file:
47
+ with open(temp_recording_file.name, "rb") as audio_file:
48
+ audio_base64 = base64.b64encode(audio_file.read()).decode('utf-8')
49
+ audio_data_uri = f"data:audio/wav;base64,{audio_base64}"
50
+ audio_player = ft.Audio(src=result_uri)
51
+ page.overlay.append(audio_player)
52
+
53
  page.update()
54
 
55
+ def play_recording(e):
56
+ if audio_player:
57
+ audio_player.play()
58
+
59
  def update_recording_status(status):
60
  recording_status.value = f"Recording status: {status}"
 
 
61
  page.update()
62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  record_button = ft.ElevatedButton("Start Recording", on_click=start_recording)
64
  stop_button = ft.ElevatedButton("Stop Recording", on_click=stop_recording, disabled=True)
65
+ play_button = ft.ElevatedButton("Play Recording", on_click=play_recording, disabled=True)
66
 
67
+ def handle_list_devices(e):
68
+ devices = audio_recorder.get_input_devices()
69
+ print(devices)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
+ def handle_has_permission(e):
72
+ try:
73
+ print(f"HasPermission: {audio_recorder.has_permission()}")
74
+ except Exception as e:
75
+ print(e)
76
+
77
+ def handle_audio_encoding_test(e):
78
+ for i in list(ft.AudioEncoder):
79
+ print(f"{i}: {audio_recorder.is_supported_encoder(i)}")
 
 
 
 
80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
  page.add(
83
+ ft.Text("Simple Audio Recorder", size=24),
84
+ ft.Row([record_button, stop_button, play_button]),
85
+ recording_status,
86
+ ft.ElevatedButton("List Devices", on_click=handle_list_devices),
87
+ ft.ElevatedButton("Test AudioEncodings", on_click=handle_audio_encoding_test),
88
+ ft.ElevatedButton("Has Permission", on_click=handle_has_permission),
 
89
  )
90
 
91
+ page.update()
 
92
 
93
  app = ft.app(target=main, export_asgi_app=True)