-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudio_player.py
More file actions
70 lines (56 loc) · 2.71 KB
/
audio_player.py
File metadata and controls
70 lines (56 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import os, asyncio, pygame
import edge_tts
class AudioPlayer:
def __init__(self, output_dir="audio_exercises"):
self.output_dir = output_dir
self.playlist = []
self.current_idx = 0
os.makedirs(self.output_dir, exist_ok=True)
# Standard Pygame mixer init works fine for this RENEWAL
pygame.mixer.init()
# Instant feedback announcements
def play_announcement(self, text):
path = os.path.join(self.output_dir, "temp_announcement.mp3")
# 1. Ensure Pygame releases the previous temp file
if pygame.mixer.music.get_busy():
pygame.mixer.music.stop()
try:
# Forces unloading of current track
pygame.mixer.music.unload()
except AttributeError:
# Fallback for old Pygame versions
pass
async def _generate():
communicate = edge_tts.Communicate(text, "de-DE-KillianNeural", rate="+0%")
await communicate.save(path)
asyncio.run(_generate())
# 2. Play immediately
pygame.mixer.music.load(path)
pygame.mixer.music.play()
# Adds solution track to playlist
def generate_and_add_tts(self, text, filename):
# Use existing length to create unique ordered tracks
safe_filename = f"{len(self.playlist)}_{filename}"
path = os.path.join(self.output_dir, f"{safe_filename}.mp3")
async def _generate():
communicate = edge_tts.Communicate(text, "de-DE-KillianNeural", rate="+0%")
await communicate.save(path)
asyncio.run(_generate())
# Append to end of playlist
self.playlist.append(path)
return path
# Media Control Navigation (used when PENDING_IMAGES is empty)
def execute_command(self, cmd):
if not self.playlist: return "Keine Playlist"
if cmd == 'next' and self.current_idx < len(self.playlist) - 1:
self.current_idx += 1
elif cmd == 'prev' and self.current_idx > 0:
self.current_idx -= 1
if cmd in ['next', 'prev', 'play']:
print(f"🔊 Loading Track {self.current_idx + 1}: {self.playlist[self.current_idx]}")
pygame.mixer.music.load(self.playlist[self.current_idx])
pygame.mixer.music.play()
elif cmd == 'pause':
if pygame.mixer.music.get_busy(): pygame.mixer.music.pause()
else: pygame.mixer.music.unpause()
return f"Track: {self.current_idx + 1} von {len(self.playlist)}"