Pyglet Player.seek() function not working?
<p>I am trying to build a simple media tool in Pyglet, which requires a seek feature. Files are loaded, paused, and then told to seek to a specific time; however, the file does not seek when Player.seek() is called. Below is the test code I am using:</p>

<pre><code>import os
import pyglet
from os.path import abspath, isfile

def loadsong(filename):
   # check for file
   print("Attempting to load "+filename)
   filename = abspath(filename)
   if not ( isfile(filename) ):
      raise Exception(filename+" not found.")
   # create a player for this file
   song = pyglet.media.load(filename)
   source = song.play()
   source.eos_action = source.EOS_LOOP
   source.pause()
   return source

music = loadsong("test.mp3")
music.seek(57)
music.play()
pyglet.app.run()
</code></pre>

<p>What am I doing wrong here? I am using Python 3.5.2, Pyglet 1.2 alpha 1, and AVBin 11 alpha 4.</p>

<p>First of all, the error you're getting is quite important.<br>
But I'll assume it's a <code>Segmentation fault (core dumped)</code> on the row:</p>

<pre><code>music.seek(12)
</code></pre>

<p>Another quick note, fix your indentation! You're using 3 spaces and whether you are a space guy or a tab guy - 3 spaces is just odd -</p>

<p>The reason for you getting a segmentation fault when trying to seek is most likely because of AVbin, it's an old (and dead afaik) project.</p>

<p>I hacked together something more similar to a music player and here's an example on how you can use <code>Seek</code> with wav files:</p>

<pre><code>import pyglet
from pyglet.gl import *
from os.path import abspath, isfile

pyglet.options['audio'] = ('pulseaudio', 'alsa', 'openal', 'silent')
pyglet.have_avbin=False
key = pyglet.window.key

def loadsong(filename):
    # check for file
    filename = abspath(filename)

    # create a player for this file
    player = pyglet.media.Player()
    player.queue(pyglet.media.load(filename, streaming=False))
    #player.play()
    #song.eos_action = song.EOS_LOOP
    #song.pause()
    return player

class main(pyglet.window.Window):
    def __init__ (self):
        super(main, self).__init__(300, 300, fullscreen = False)
        self.alive = 1

        self.player = loadsong('./test.wav')

    def on_draw(self):
        self.render()

    def on_close(self):
        self.alive = 0

    def on_key_press(self, symbol, modifiers):
        if symbol == key.ESCAPE: # [ESC]
            self.alive = 0
        elif symbol == key.SPACE:
            if self.player.playing:
                self.player.pause()
            else:
                self.player.play()
        elif symbol == key.RIGHT:
            print('Skipping to:',self.player.time+2)
            self.player.source.seek(self.player.time+2)
        elif symbol == key.LEFT:
            print('Rewinding to:',self.player.time-2)
            self.player.source.seek(self.player.time-2)

    def render(self):
        self.clear()

        #source = pyglet.text.Label(str(self.player.source.info.title.decode('UTF-8')), x=20, y=300-30)
        volume = pyglet.text.Label(str(self.player.volume*100)+'% volume', x=20, y=40)
        p_time = pyglet.text.Label(str(self.player.time), x=20, y=20)

        #source.draw()
        volume.draw()
        p_time.draw()

        self.flip()

    def run(self):
        while self.alive == 1:
            self.render()

            # -----------&gt; This is key &lt;----------
            # This is what replaces pyglet.app.run()
            # but is required for the GUI to not freeze
            #
            event = self.dispatch_events()

x = main()
x.run()
</code></pre>

<p>A few important notes:</p>

<pre><code>pyglet.have_avbin=False
</code></pre>

<p>This will turn off AVbin completely, there is probably a way to turn it off for individual sources.. But since I rarely play around with it I honestly have no idea of how to.. So off it goes :)</p>

<p>Secondly:</p>

<pre><code>streaming=False
</code></pre>

<p>On the <code>media.load()</code> call is quite important, otherwise you might get weird artifacts in your sound and or no sound at all. I got instance to a super high pitched scratching noise that almost made me deaf without that flag.</p>

<p>Other than that, the code is quite straight forward.</p>

<pre><code>self.player.source.seek(&lt;time&gt;)
</code></pre>

<p>Is called on the player object that is a instance of <code>pyglet.media.Player()</code>. And it works brilliantly.</p>

<p>Another work-around would be to manually install <a href="https://code.google.com/archive/p/avbin/" rel="nofollow">AVbin 7</a> which <strong>appears</strong> to be working better, but I'm reluctant to install it on this machine just for testing purposes.. But the overall info i've gathered over the years is that that old library works better with Mp3 files.</p>

python python-3.x media pyglet