1

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

Nicholas
  • 71
  • 9
  • Have you tried splitting the original file into 10s clips, and playing those clips sequentially? – David Culbreth Jun 12 '18 at 15:01
  • @Nicholas, this is an interesting question but make sure you provide an [mcve](https://stackoverflow.com/help/mcve) so we can see what you've acomplished so far and at what point you've got stuck. Otherwise people will start downvoting you... Here's a motivational thumb up :) – BPL Jun 12 '18 at 15:03
  • @BPL what is mcve? – Nicholas Jun 12 '18 at 15:04
  • @DavidCulbreth I'll edit my post to give what I accomplish so far – Nicholas Jun 12 '18 at 15:05
  • @Nicholas Here's the links of [mcve](https://stackoverflow.com/help/mcve) or [sscce](http://sscce.org/) – BPL Jun 12 '18 at 15:06
  • @BPL Thanks, I've added my work so far – Nicholas Jun 12 '18 at 15:12
  • @DavidCulbreth This is my work so far, I've already edited it – Nicholas Jun 12 '18 at 15:13
  • @Nicholas I've been researching about the options here (GStreamer, pymedia, ...) and all of them got some issues when trying to use python3.6 on win/nix . Right now I was testing [pyglet](https://stackoverflow.com/questions/45842385/how-can-i-play-audio-stream-without-saving-it-into-the-file-with-pyglet) but haven't figured out yet how to make it work. Mentioning in case you're interested on it – BPL Jun 12 '18 at 17:04
  • @BPL thank you so much for helping me!!, Right now, I was testing pyaudio and still got an issue that some part is missing when i play it – Nicholas Jun 12 '18 at 18:25
  • @Nicholas Well, your question is interesting, the idea of streaming internet audio to do some audio processing or other stuff can be nice. Thing is, I've seen several related SO threads like [this](https://stackoverflow.com/questions/38171169/how-to-play-mp3-from-url), [this](https://stackoverflow.com/questions/46758360/how-to-play-streaming-audio-from-internet-radio-on-python-3-5-3?rq=1) and others and none of them provided nice solutions that worked out of the box with 3.6.x/win/nix , that's why I'm walking the pyglet+(requests|urllib) road right now – BPL Jun 12 '18 at 18:54
  • This might help you split the files. https://stackoverflow.com/q/2952309/4802259 – David Culbreth Jun 12 '18 at 19:23
  • @BPL Thanks bro for giving me those threads those are similar. I've read them all and still don't know how to solve the packet loss!! Eventually, I can play the parts right now, but still I need to solve the problem that some of parts is missing!! – Nicholas Jun 13 '18 at 01:31
  • @Nicholas If I knew how to answer your question I would have already done... I've opened a similar [related thread](https://stackoverflow.com/questions/50826002/how-to-play-streaming-audio-using-pyglet) to this one btw, although is asking about pyglet specifically and not pyaudio+pydub. Posting here so you can mark it as favourite in case anyone answers there ;) – BPL Jun 13 '18 at 07:05

0 Answers0