Administrator
2024-04-26 c4861d2429c2bf3a3f11309ad879b549e62e722d
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
import time
 
import pygame
 
 
# 报警
import constant
from utils import tool
 
 
def alarm():
    if not tool.is_alert_time() or constant.TEST:
        return
    if not constant.NEED_ALERT:
        return
    AlertUtil().stop_audio()
    AlertUtil().play_audio()
 
 
class AlertUtil:
    __instance = None
 
    # 单例模式
    def __new__(cls, *args, **kwargs):
        if not cls.__instance:
            cls.__instance = super(AlertUtil, cls).__new__(cls, *args, **kwargs)
            # 初始化设置
            pygame.mixer.init()
            pygame.mixer.music.load('../alert.mp3')
            pygame.mixer.music.set_volume(1)
        return cls.__instance
 
    def play_audio(self):
        pygame.mixer.music.play()
 
    def stop_audio(self):
        pygame.mixer.music.stop()
 
 
if __name__ == '__main__':
    alarm()
    time.sleep(2)