2016-09-19 41 views
2

我想在Pyglet中构建一个简单的媒体工具,它需要一个搜索功能。文件被加载,暂停,然后被告知寻找特定的时间;但是,当调用Player.seek()时,该文件不会查找。以下是我正在使用的测试代码:Pyglet Player.seek()函数不工作?

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() 

我在做什么错在这里?我正在使用Python 3.5.2,Pyglet 1.2 alpha 1和AVBin 11 alpha 4.

回答

0

首先,您遇到的错误非常重要。
但我会以为这是该行一个Segmentation fault (core dumped)

music.seek(12) 

另一个要点,解决您的压痕!您正在使用3个空格,无论您是空间人员还是选项卡人员 - 3个空格都是奇数 -

您尝试寻找时出现分段错误的原因很可能是因为AVbin,这是旧的(和死亡afaik)项目。

我砍死在一起,更类似于一个音乐播放器,这里是你如何使用Seek与WAV文件的示例东西:

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() 

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

x = main() 
x.run() 

几项重要说明:

pyglet.have_avbin=False 

这将打开完全关闭AVbin,可能有一种方法可以关闭个人来源..但是由于我很少玩弄它,所以我实在不知道该怎么做。所以关掉它:)

其次:

streaming=False 

media.load()调用是非常重要的,否则你可能会得到你的声音和或根本没有声音怪异的文物。我遇到了一个超级高scrat scrat的噪音,几乎让我失去了这面旗帜。

除此之外,代码非常简单。

self.player.source.seek(<time>) 

被作为pyglet.media.Player()的实例的播放器对象调用。它的出色表现非常出色。

另一种变通方法是手动安装AVbin 7似乎是更好的工作,但我不愿在此机器上安装它只是为了测试目的。但总体信息我已经收集过多年以来,这个老图书馆对Mp3文件的效果更好。

+0

它实际上没有给我任何的错误可言的,肯定不是段错误。该文件播放良好,它只是拒绝实际寻找。有没有办法使用与.mp3文件保持兼容的解决方法?将所有音乐重新编码为.wav是不可行的。 –

+0

@ A.Vist这个文件在这里也很好,直到我试图寻找它。然后突然之间我得到一个段错误(在Linux上)。无法真正了解为什么会发生这种情况,但如果上述代码没有解决问题并且无法使用.wav,我最好的猜测就是在他们的bitbucket页面上提交错误报告。 – Torxed

0

在调用Player.seek()之前,请确保Player.playing为True。 在loadsong()函数的最后,您调用了将会将Player.playing设置为False的pause()。

你可以尝试:代替

music.play() 
music.seek(57) 

music.seek(57) 
music.play()