import pygame
import socket
# set UDP port
UDP_PORT = 5005
# audio (.wav) file path
AUDIO_FILE_PATH = "/home/rpi/mixkit-emergency-alert-alarm-1007.wav"
def play_audio(file_path):
pygame.mixer.init()
pygame.mixer.music.load(file_path)
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
continue
if __name__ == "__main__":
# start UDP socket UDP
udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udp_socket.bind(("", UDP_PORT))
print("Listening UDP port", UDP_PORT)
while True:
data, addr = udp_socket.recvfrom(1024)
message = data.decode("utf-8")
print("Message received:", message)
# if msg received, play audio file
if message == "play_audio":
play_audio(AUDIO_FILE_PATH)