forked from DaniLionn/ACNLMusicApp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
64 lines (51 loc) · 1.48 KB
/
main.py
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
import os
import time
from datetime import datetime
from pygame import mixer # For audio playback
# Initialize pygame mixer
mixer.init()
# Define base audio directory
def get_audio_path():
return os.path.join(os.path.dirname(__file__), "audios")
audio_dir = get_audio_path()
# Map hours to corresponding audio files
audios = [
os.path.join(audio_dir, f"{hour}AM.mp3") if hour < 12 else os.path.join(audio_dir, f"{hour % 12 or 12}PM.mp3")
for hour in range(24)
]
# Playback state
playing = False
last_hour = -1
current_audio = None
# Function to play audio file
def play_audio(file_path):
global playing, current_audio
try:
if current_audio:
current_audio.stop()
mixer.music.load(file_path)
mixer.music.play()
playing = True
except Exception as e:
print(f"Error playing audio: {e}")
playing = False
try:
while True:
now = datetime.now()
current_hour = now.hour
current_time = now.strftime("%I:%M:%S %p")
# Display the current time
os.system("clear" if os.name == "posix" else "cls")
print("Playing!")
print(f"It is currently {current_time}")
# Check if the hour has changed
if current_hour != last_hour:
last_hour = current_hour
play_audio(audios[current_hour])
time.sleep(1)
except KeyboardInterrupt:
print("Exiting...")
except Exception as e:
print(f"An error occurred: {e}")
finally:
mixer.quit()