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
| import time
|
| import pygame
|
|
| # 报警
| def alarm():
| 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)
|
|