How to play streaming audio using pyglet? : <p>The goal of this question is trying to figure out how to play streaming audio using pyglet. The first is just making sure you're able to play mp3 files using pyglet, that's the purpose of this first snippet:</p>

<pre><code>import sys
import inspect
import requests

import pyglet
from pyglet.media import *

pyglet.lib.load_library('avbin')
pyglet.have_avbin = True


def url_to_filename(url):
    return url.split('/')[-1]


def download_file(url, filename=None):
    filename = filename or url_to_filename(url)

    with open(filename, "wb") as f:
        print("Downloading %s" % filename)
        response = requests.get(url, stream=True)
        total_length = response.headers.get('content-length')

        if total_length is None:
            f.write(response.content)
        else:
            dl = 0
            total_length = int(total_length)
            for data in response.iter_content(chunk_size=4096):
                dl += len(data)
                f.write(data)
                done = int(50 * dl / total_length)
                sys.stdout.write("\r[%s%s]" % ('=' * done, ' ' * (50 - done)))
                sys.stdout.flush()


url = "https://freemusicarchive.org/file/music/ccCommunity/DASK/Abiogenesis/DASK_-_08_-_Protocell.mp3"
filename = "mcve.mp3"
download_file(url, filename)

music = pyglet.media.load(filename)
music.play()
pyglet.app.run()
</code></pre>

<p>If you've installed the libraries <code>pip install pyglet requests</code> and also installed <a href="https://avbin.github.io/AVbin/Download.html" rel="noreferrer">AVBin</a> at this point you should be able to listen the mp3 once it's been downloaded.</p>

<p>Once we've reached this point, I'd like to figure out how to play &amp; buffering the file in a similar way to mostly of the existing web video/audio players using pyglet+requests. This means playing the files without waiting till the file has been downloaded completely.</p>

<p>After reading the pyglet media <a href="http://pyglet.readthedocs.io/en/pyglet-1.3-maintenance/modules/media.html#classes" rel="noreferrer">docs</a> you can see there are available these classes:</p>

<pre><code>media
    sources
        base
            AudioData
            AudioFormat
            Source
            SourceGroup
            SourceInfo
            StaticSource
            StreamingSource
            VideoFormat
    player
        Player
        PlayerGroup
</code></pre>

<p>I've seen there are another similar SO questions but they haven't been solved properly and their content doesn't provide a lot of relevant details:</p>

<ul>
<li><a href="https://stackoverflow.com/questions/11344548/play-streaming-audio-using-pyglet">Play streaming audio using pyglet</a></li>
<li><a href="https://stackoverflow.com/questions/45842385/how-can-i-play-audio-stream-without-saving-it-into-the-file-with-pyglet">How can I play audio stream without saving it into the file with pyglet?</a></li>
</ul>

<p>That's why I've created a new question. How do you play streaming audio using pyglet? Could you provide a little example using the above mcve as a base?</p>
