Well, I tried to write something, it works for me at least but partially, I will apriciete if someone look at the code and tell me whats wrong and what could be better. I tried first with mplayer -slave -idle -quit and than sending signals to stdin but I find this very slow, progress bar was not smooth because I have to use atleast time.sleep(0.05) to wait for mplayer, then I removed the -quit and tried to parse mplayers output but then, I dont know how to watch for eof so I can not requestnextsong... How do I do that? Thx
mplayer.py
import sys import os import fcntl import string
from services.player import genericplayer
def makeNonBlocking(fd): fl = fcntl.fcntl(fd, fcntl.F_GETFL) fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)
class player(genericplayer): def __init__(self, id, playlistid, autoplay, cmdline): self.cmdline = cmdline self.initmplayer() genericplayer.__init__(self, id, playlistid, autoplay)
def initmplayer(self): """start new mplayer process""" cmd = self.cmdline + " -vo null -slave -idle" self.pstdin, self.pstdout = os.popen4(cmd) makeNonBlocking(self.pstdout.fileno())
def closemplayer(self): """terminate running mplayer process""" self.sendmplayer("quit") if self.pstdin: self.pstdin.close() if self.pstdout: self.pstdout.close()
def sendmplayer(self, command): """send command to mplayer process""" try: self.pstdin.write("%s\n" % command) self.pstdin.flush() except IOError, error: # broken pipe => restart player if error[0]==32: self.closemplayer() self.initmplayer() self.playbackinfo.stopped() self.pstdin.write("%s\n" % command) else: raise
def receivemplayer(self): """receive command from mplayer process""" try: return self.pstdout.read() except (ValueError, IOError): return "error"
def play(self): """play songs"""
r = self.receivemplayer()
if r.startswith("A:"): ptime = string.split(r)[1] ptime = int(float(ptime)) self.playbackinfo.updatetime(ptime) elif r != "error" and r=="": if self.playbackinfo.isplaying(): self.playbackinfo.stopped() self.requestnextsong()
def _playsong(self, song, manual): """play event.song next""" self.sendmplayer("loadfile "%s" 0" % song.path) self.playbackinfo.updatesong(song)
def _playerunpause(self): """unpause playing""" self.sendmplayer("pause") self.playbackinfo.playing()
def _playerpause(self): """pause playing""" self.sendmplayer("pause") self.playbackinfo.paused()
def _playerseekrelative(self, seconds): self.sendmplayer("seek %+d" % int(seconds))
def _playerstop(self): """stop playing""" self.closemplayer() self.initmplayer() self.playbackinfo.stopped()
def _playerquit(self): self.sendmplayer("quit") self.closemplayer()
= Roseville Home Finder Roseville Home for sale. All you need to find your dream home in Roseville. Create your own MLS search, obtain free buyer and seller reports, obtain area information, and much more here. http://a8-asy.a8ww.net/a8-ads/adftrclick?redirectid=99578ea4ef3b65667d260d13...