Administrator
2023-03-27 8535f56dbf6e410b4a09f02f95d4d49bcc8753f2
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
import tool
 
 
def alarm():
    return
    if not tool.is_alert_time() or constant.TEST:
        return
    # TODO 暂时关闭报警
    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)