import socket
import struct
import pyaudio
import time

p = pyaudio.PyAudio()

sampwidth = 2
nchannels = 2
framerate = 44100

stream = p.open(format = pyaudio.paInt16,
                channels = nchannels,
                rate = framerate,
                output = True)

MCAST_GRP = '224.1.1.1'
MCAST_PORT = 5007
IS_ALL_GROUPS = True

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
if IS_ALL_GROUPS:
    # on this port, receives ALL multicast groups
    sock.bind(('', MCAST_PORT))
else:
    # on this port, listen ONLY to MCAST_GRP
    sock.bind((MCAST_GRP, MCAST_PORT))
mreq = struct.pack("4sl", socket.inet_aton(MCAST_GRP), socket.INADDR_ANY)

sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)

data = sock.recv(4096)
while data!=b'':
  data = sock.recv(4096)
  stream.write(data)time.sleep(0.22)

