33# Supports I2S (digital audio) and PWM buzzer (tones/ringtones)
44#
55# Simple routing: play_wav() -> I2S, play_rtttl() -> buzzer
6- # Uses TaskManager (asyncio) for non-blocking background playback
6+ # Uses _thread for non-blocking background playback (separate thread from UI)
77
8- from mpos .task_manager import TaskManager
8+ import _thread
9+ import mpos .apps
910
1011# Stream type constants (priority order: higher number = higher priority)
1112STREAM_MUSIC = 0 # Background music (lowest priority)
1617_i2s_pins = None # I2S pin configuration dict (created per-stream)
1718_buzzer_instance = None # PWM buzzer instance
1819_current_stream = None # Currently playing stream
19- _current_task = None # Currently running playback task
2020_volume = 50 # System volume (0-100)
2121
2222
@@ -86,27 +86,27 @@ def _check_audio_focus(stream_type):
8686 return True
8787
8888
89- async def _playback_coroutine (stream ):
89+ def _playback_thread (stream ):
9090 """
91- Async coroutine for audio playback.
91+ Thread function for audio playback.
92+ Runs in a separate thread to avoid blocking the UI.
9293
9394 Args:
9495 stream: Stream instance (WAVStream or RTTTLStream)
9596 """
96- global _current_stream , _current_task
97+ global _current_stream
9798
9899 _current_stream = stream
99100
100101 try :
101- # Run async playback
102- await stream .play_async ()
102+ # Run synchronous playback in this thread
103+ stream .play ()
103104 except Exception as e :
104105 print (f"AudioFlinger: Playback error: { e } " )
105106 finally :
106107 # Clear current stream
107108 if _current_stream == stream :
108109 _current_stream = None
109- _current_task = None
110110
111111
112112def play_wav (file_path , stream_type = STREAM_MUSIC , volume = None , on_complete = None ):
@@ -122,8 +122,6 @@ def play_wav(file_path, stream_type=STREAM_MUSIC, volume=None, on_complete=None)
122122 Returns:
123123 bool: True if playback started, False if rejected or unavailable
124124 """
125- global _current_task
126-
127125 if not _i2s_pins :
128126 print ("AudioFlinger: play_wav() failed - I2S not configured" )
129127 return False
@@ -132,7 +130,7 @@ def play_wav(file_path, stream_type=STREAM_MUSIC, volume=None, on_complete=None)
132130 if not _check_audio_focus (stream_type ):
133131 return False
134132
135- # Create stream and start playback as async task
133+ # Create stream and start playback in separate thread
136134 try :
137135 from mpos .audio .stream_wav import WAVStream
138136
@@ -144,7 +142,8 @@ def play_wav(file_path, stream_type=STREAM_MUSIC, volume=None, on_complete=None)
144142 on_complete = on_complete
145143 )
146144
147- _current_task = TaskManager .create_task (_playback_coroutine (stream ))
145+ _thread .stack_size (mpos .apps .good_stack_size ())
146+ _thread .start_new_thread (_playback_thread , (stream ,))
148147 return True
149148
150149 except Exception as e :
@@ -165,8 +164,6 @@ def play_rtttl(rtttl_string, stream_type=STREAM_NOTIFICATION, volume=None, on_co
165164 Returns:
166165 bool: True if playback started, False if rejected or unavailable
167166 """
168- global _current_task
169-
170167 if not _buzzer_instance :
171168 print ("AudioFlinger: play_rtttl() failed - buzzer not configured" )
172169 return False
@@ -175,7 +172,7 @@ def play_rtttl(rtttl_string, stream_type=STREAM_NOTIFICATION, volume=None, on_co
175172 if not _check_audio_focus (stream_type ):
176173 return False
177174
178- # Create stream and start playback as async task
175+ # Create stream and start playback in separate thread
179176 try :
180177 from mpos .audio .stream_rtttl import RTTTLStream
181178
@@ -187,7 +184,8 @@ def play_rtttl(rtttl_string, stream_type=STREAM_NOTIFICATION, volume=None, on_co
187184 on_complete = on_complete
188185 )
189186
190- _current_task = TaskManager .create_task (_playback_coroutine (stream ))
187+ _thread .stack_size (mpos .apps .good_stack_size ())
188+ _thread .start_new_thread (_playback_thread , (stream ,))
191189 return True
192190
193191 except Exception as e :
@@ -197,7 +195,7 @@ def play_rtttl(rtttl_string, stream_type=STREAM_NOTIFICATION, volume=None, on_co
197195
198196def stop ():
199197 """Stop current audio playback."""
200- global _current_stream , _current_task
198+ global _current_stream
201199
202200 if _current_stream :
203201 _current_stream .stop ()
0 commit comments