So I want to create a simple program like music streaming player using UDP socket protocol. So far, I can read the mp3 file, split it into some bytes, and send it continuously to my receiver (client). My problem is I want to play the mp3 file part by part while receiving it from the server. Can Anyone give me idea how to do this? I've tried to play the part after receiving it, but there is transition between part. I want to play it smoothly just like spotify.
Note : I don't want to receive all parts, join them, and play it. What I want is play the part while receiving it. Thank you so much for the help. I appreciate it
My code so far :
I sent the split file through UDP by this code
with open('test.mp3', 'rb') as infile:
d = infile.read(65500)
while d :
time.sleep(1)
sent = sock.sendto(d, address)
d = infile.read(65500)
I receive the split file by this code
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paInt16,
channels=10,
rate=44100,
output=True)
data, server = (sock.recvfrom(65500))
while data != 'stop':
stream.write(data)
data, server = (sock.recvfrom(65500))
stream.stop_stream()
stream.close()
p.terminate()
I've tried this code and I got buzzing sound.
another approach is :
while 1:
data, server = (sock.recvfrom(65500))
song = AudioSegment.from_file(io.BytesIO(data), format="mp3")
play(song) #with pydub
This code is work, but as you know I play each part separately, so there is a delay between each part, and some part of the song is missing