move conn.stream to another thread
This commit is contained in:
1 parent
f195cfbbd8
commit
ce58103295
1 file changed
+16
-9
+16
-9
@@ -1,6 +1,7 @@
|
|||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import io
|
import io
|
||||||
|
import asyncio
|
||||||
|
|
||||||
LOG_FILE = 'textual.log'
|
LOG_FILE = 'textual.log'
|
||||||
LOGGING_LEVEL = logging.DEBUG
|
LOGGING_LEVEL = logging.DEBUG
|
||||||
@@ -498,18 +499,19 @@ class TextualSubpyPlayer(App):
|
|||||||
audio_data = None
|
audio_data = None
|
||||||
audio_cache_path = f'.cache/audio/{song.id}.ogg'
|
audio_cache_path = f'.cache/audio/{song.id}.ogg'
|
||||||
|
|
||||||
if CACHE:
|
if CACHE and os.path.exists(audio_cache_path):
|
||||||
if os.path.exists(audio_cache_path):
|
with open(audio_cache_path, 'rb') as file:
|
||||||
with open(audio_cache_path, 'rb') as file:
|
audio_data = io.BytesIO(file.read())
|
||||||
audio_data = io.BytesIO(file.read())
|
else:
|
||||||
|
# Run the blocking stream call in a separate thread
|
||||||
|
r = await asyncio.to_thread(conn.stream, song.id)
|
||||||
|
audio_content = r.content
|
||||||
|
|
||||||
if not audio_data:
|
|
||||||
r = conn.stream(song.id)
|
|
||||||
if CACHE:
|
if CACHE:
|
||||||
with open(audio_cache_path, "wb") as file:
|
# Also run the file writing in a thread
|
||||||
file.write(r.content)
|
await asyncio.to_thread(self._cache_audio, audio_cache_path, audio_content)
|
||||||
|
|
||||||
audio_data = io.BytesIO(r.content)
|
audio_data = io.BytesIO(audio_content)
|
||||||
|
|
||||||
pygame.mixer.music.load(audio_data)
|
pygame.mixer.music.load(audio_data)
|
||||||
pygame.mixer.music.play()
|
pygame.mixer.music.play()
|
||||||
@@ -526,6 +528,11 @@ class TextualSubpyPlayer(App):
|
|||||||
# Set callback loop for progress bar
|
# Set callback loop for progress bar
|
||||||
# self.progress_timer = self.set_interval(1 / 10, self.update_progress, pause=False)
|
# self.progress_timer = self.set_interval(1 / 10, self.update_progress, pause=False)
|
||||||
|
|
||||||
|
def _cache_audio(self, path: str, content: bytes) -> None:
|
||||||
|
"""Helper method to write audio to cache."""
|
||||||
|
with open(path, "wb") as file:
|
||||||
|
file.write(content)
|
||||||
|
|
||||||
def check_done_playing(self) -> None:
|
def check_done_playing(self) -> None:
|
||||||
if not pygame.mixer.music.get_busy() and not self.is_paused:
|
if not pygame.mixer.music.get_busy() and not self.is_paused:
|
||||||
self.play_history.append(self.currently_playing)
|
self.play_history.append(self.currently_playing)
|
||||||
|
|||||||
Reference in new issue
Block a user