Guest User

web2py thread playter

a guest
Feb 26th, 2012
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.99 KB | None | 0 0
  1. # file: player.py
  2.  
  3. import threading
  4. from subprocess import Popen, PIPE
  5. from datetime import datetime
  6.  
  7.  
  8.  
  9. class MPG123Player(threading.Thread):
  10.     daemon = True # added to make Ctrl-C and Ctrl-D work when running python -i player.py
  11.  
  12.     def __init__(self, music=''):
  13.         self.music = music
  14.         threading.Thread.__init__(self)
  15.         self._kill_me = False
  16.         self.player = self.init_player()
  17.  
  18.     def finish_it(self):
  19.         self._kill_me = True
  20.  
  21.     def init_player(self):
  22.         return Popen(['mpg123', '--remote', self.music], shell=False, stdout=PIPE, stdin=PIPE)
  23.  
  24.     def run(self):
  25.         '''Thread method that is called when a Thread is started,
  26.        this is the "main loop" of it'''
  27.         try:
  28.             self.player_loop()
  29.         finally:
  30.             self.quit()
  31.  
  32.     def play(self, music=''):
  33.         music = music or self.music
  34.         if music:
  35.             cmd = 'LOAD ' + music
  36.             self.player_cmd(cmd)
  37.  
  38.     def stop(self):
  39.         self.player_cmd('STOP')
  40.  
  41.     def player_cmd(self, cmd):
  42.         '''Protocol to talk to the mpg123 '''
  43.         self.player.stdin.write(cmd + '\n')
  44.  
  45.     def quit(self):
  46.         self.player.terminate()
  47.  
  48.     def player_loop(self):
  49.         player = self.player
  50.  
  51.         self.play()
  52.  
  53.         while not self._kill_me:
  54.             status = player.stdout.readline()
  55.             '''here we have to keep reading the stdout, because if we don't
  56.            read from it this buffer get full and the mpg123 stops working.
  57.  
  58.            I give from it because of this issue, mplayer.py has a better approach
  59.            '''
  60.             #print status
  61.             if status.startswith('@F'):
  62.                 cmd_name, cur_frame,  frames_left, cur_seconds, seconds_left = status.split()
  63.                 if cmd_name == '@F' and cur_seconds == '0.00':
  64.                     self.total_seconds = float(seconds_left)
  65.                 cur_seconds = float(cur_seconds)
  66.                 self.cur_seconds = cur_seconds
  67.  
  68.  
  69. class Mplayer(threading.Thread, BasePlayer):
  70.     daemon = True # added to make Ctrl-C and Ctrl-D work when running python -i player.py
  71.  
  72.     def __init__(self, music=''):
  73.         threading.Thread.__init__(self)
  74.  
  75.     def init_player(self):
  76.         # it is using mplayer.py - https://github.com/baudm/mplayer.py
  77.         return mplayer.Player(stderr=subprocess.PIPE)
  78.         # but the basic idea is run the mplayer with this options, but
  79.         # mplayer.py has some convenient api
  80.         #
  81.         #return Popen(['mplayer', '-slave', '-idle', #'-msglevel', 'global=4',
  82.         #            '-quiet', 'nodefault-bindings', '-noconfig', 'all',
  83.         #            self.music], shell=False, stdout=PIPE, stdin=PIPE,
  84.         #            stderr=subprocess.STDOUT)
  85.  
  86.     def run(self):
  87.         try:
  88.             self.player_loop()
  89.         finally:
  90.             self.quit()
  91.  
  92.     def play(self, music=''):
  93.         music = music or self.music
  94.         if music:
  95.             cmd = self.player.loadfile(music)
  96.     def stop(self):
  97.         self.player.stop()
  98.  
  99.     def quit(self):
  100.         self.player.quit()
  101.         #self.player.terminate()
  102.  
  103.     def player_loop(self):
  104.         player = self.player
  105.  
  106.         while not self._kill_me:
  107.             self.total_seconds = player.length or 0.0
  108.             self.cur_seconds = player.time_pos or 0.0
  109.  
  110.             if (self.total_seconds - self.cur_seconds) <= 0.05:
  111.                 # if it's about to finish the music dispatch the logic to get
  112.                 # the next music
  113.                 print '[%s] ended:' % datetime.now(), player.filename
  114.  
  115.  
  116. player = Mplayer('/path/to/a/file/199.mp3')
  117. #player = MPG123Player('199.mp3')
  118.  
  119. player.start()
  120.  
  121.  
  122. # you can play with the player using python console
  123. # python -i player.py
  124. # >>> player.play('file')
  125. # >>> player.stop()
  126. # I put this file in my application/myapp/modules/player.py
  127. # and in my controllers I can from player import player
  128. # def index()
  129. #     return '%s of %s' % (player.cur_seconds, player.total_seconds)
Advertisement
Add Comment
Please, Sign In to add comment