2

Now I have these libraries: requests, pyglet, pyaudio

How can I play an audio stream using ones, for example, from this site without saving it into the file(using buffering)?

There is a confusing information in documentation of this library about a StreamingSource class

When I push the information in bytes in StreamingSource object(source.get_audio_data(DATA)) and after that I push this one into a Player(pyglet.media.Player()) it throws an exception, that says that the StreamingSource hasn't attribute duration

Code:

import pyglet, requests

req = requests.get('http://ic7.101.ru:8000/c15_3', stream=True)

player = pyglet.media.Player()

source = pyglet.media.StreamingSource()

CHUNK = 1024

for num, chunk in enumerate(req.iter_content(CHUNK)):
    if num == 1000:
        break
    source.get_audio_data(chunk)
    if num == 100:
        player.queue(source)
        player.play()
        pyglet.app.run()
pyglet.clock.schedule_once(lambda dt: pyglet.app.exit(), source.duration)

Traceback:

Traceback (most recent call last):
  File "/home/user/.PyCharmCE2017.1/config/scratches/scratch.py", line 16, in <module>
    player.queue(source)
  File "/usr/local/lib/python3.5/dist-packages/pyglet/media/__init__.py", line 978, in queue
    group.queue(source)
  File "/usr/local/lib/python3.5/dist-packages/pyglet/media/__init__.py", line 698, in queue
    self.duration += source.duration
TypeError: unsupported operand type(s) for +=: 'float' and 'NoneType'
sakost
  • 250
  • 4
  • 15
  • Do you absolutely need pyglet or you are open to other libraries? – Anil_M Sep 08 '17 at 22:25
  • @Anil_M, I want to know, how it can works.. – sakost Sep 19 '17 at 14:56
  • Check my answer. Solution is based on `python-vlc` library. – Anil_M Sep 19 '17 at 20:10
  • @MrMorgan I'm curious, you've asked about a pyglet solution but you've validated a python-vlc based solution? How so? :/ – BPL Jun 12 '18 at 15:40
  • @BPL , check the title of my question, pls) – sakost Jun 20 '18 at 20:50
  • @MrMorgan Am I missing something here? Your title says `How can I play audio stream without saving it into the file with pyglet?` and you've validated an answer who talks about `python-vlc`. python-vlc isn't pyglet... That said, few days ago I've opened an alternative question [https://stackoverflow.com/questions/50826002/how-to-play-streaming-audio-using-pyglet](https://stackoverflow.com/questions/50826002/how-to-play-streaming-audio-using-pyglet) but nobody has been able to answer it so far :/ – BPL Jun 20 '18 at 22:23
  • Possible duplicate of [How to play streaming audio using pyglet?](https://stackoverflow.com/questions/50826002/how-to-play-streaming-audio-using-pyglet) – Munim Munna Jun 28 '18 at 07:38
  • @MunimMunna, before making such statement, check at least references from the question that you show. it has a reference to this issue – sakost Jun 28 '18 at 15:15

1 Answers1

1

If you just want to play a file (audio/video) from a url without saving, you can use vlc as below.
Details on vlc are here

You can install vlc (on windows) as

pip install python-vlc

Source Code

import vlc

url = 'http://ic7.101.ru:8000/c15_3'
#define VLC instance
instance = vlc.Instance('--input-repeat=-1', '--fullscreen')

#Define VLC player
player=instance.media_player_new()

#Define VLC media
media=instance.media_new(url)

#Set player media
player.set_media(media)

#Play the media
player.play()
Anil_M
  • 10,893
  • 6
  • 47
  • 74