Search code examples
pythonloadingpyglet

How to increase loading speed for pyglet media


I'm making a MediaPlayer using python and pyglet. At the moment I can load any audio file just as intended. However I've run into an issue where pyglet only seems to load files at a speed of 2.4MBps. Now this is fine for 5min songs. However I'd like to be able to use it for audiobooks as well. These can be 10+ hours long and over 500MB in size.

There are two problems with those right now.

  1. It takes forever to load (windows already marks the window with "No response"). It will load eventually and play just fine, it just takes too long.
  2. The player itself starts eating up RAM. Loading a 700MB audiobook resulted in the application using almost 5GB of RAM.

Is there a way to fix these problems?

Not sure if my code would help, but these are the lines I assume are most relevant

import pyglet

self.media = pyglet.media.load(path, streaming=False)
self.player = pyglet.media.Player()
self.player.queue(self.media)
self.player.play()

Path to file (usually .m4a) is provided by other parts of the program


Solution

  • streaming = True is the solution for what you are doing. Setting streaming to False turns it into a StaticSource. A StaticSource loads the entire uncompressed file in it's entirety into memory, which is not viable for large files. Streaming only takes chunks of the data at a time, and decodes it as it plays. This uses a lot less memory.

    Generally a StaticSource is for small sounds that are played frequently or multiple times at once. If you had a multiple balls bouncing around and wanted to play a bounce sound each time they landed, then yes you would disable streaming.

    If you want to load multiple audio books and play them in succession, then you would just player queue more media sources.