player.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!./bin/python3
  2. import time
  3. import threading
  4. from display import Display
  5. from typing import Dict
  6. from pymplb import MPlayer
  7. class Player(Display):
  8. '''
  9. Player object which interacts with mplayer.
  10. '''
  11. def __init__(self, config, pydsp):
  12. super().__init__({
  13. "dimx" : config.get("dimx"),
  14. "dimx" : config.get("dimy"),
  15. "playlist" : config.get("dimy"),
  16. "fontsize" : config.get("fontsize"),
  17. }, pydsp)
  18. self.playing = False # Flag if the player is playing
  19. self._playingPlaylist = None # Current playing order
  20. self._data = [] # Current playlist
  21. self._player = MPlayer() # Reference to mplayer control object
  22. threading.Thread(target=self._updateFromPlayer, daemon=True).start()
  23. def _updateFromPlayer(self):
  24. '''
  25. Thread to update the player display from the state of mplayer.
  26. '''
  27. while True:
  28. try:
  29. time.sleep(0.5)
  30. if self.playing:
  31. player_playing = self._player.p_path
  32. # Update playlist display if the current song has changed
  33. # and the item can be found in the current playlist
  34. if player_playing != self._current_item.get("path"):
  35. for i, item in enumerate(self._data):
  36. if item.get("path") == player_playing:
  37. self._selection_pointer = i
  38. self._current_item = item
  39. self.update()
  40. # Start the playlist again if the player ran out of items
  41. if player_playing == "(null)":
  42. for item in self._playingPlaylist:
  43. self._player.loadfile(item["path"], 1)
  44. except Exception as e:
  45. print("Error update from player:", e)
  46. def setPlaylist(self, data: Dict):
  47. '''
  48. Set the playlist for this player.
  49. '''
  50. self._selection_pointer = 0
  51. self._data = data
  52. def getPlaylist(self) -> Dict:
  53. '''
  54. Return the playlist for this display.
  55. '''
  56. return self._data
  57. def togglePlay(self):
  58. '''
  59. Toggle playing the selected item.
  60. '''
  61. if not self._current_item:
  62. return
  63. if self._player.p_path == self._current_item["path"]:
  64. print("stop")
  65. self.playing = False
  66. self._player.stop()
  67. else:
  68. ci = self._current_item["path"]
  69. self._playingPlaylist = []
  70. add = 0
  71. for item in self._data:
  72. if item == self._current_item:
  73. self.playing = True
  74. self._player.loadfile(item["path"])
  75. self._playingPlaylist.append(item)
  76. add = 1
  77. elif add == 1:
  78. self._player.loadfile(item["path"], 1)
  79. self._playingPlaylist.append(item)
  80. for item in self._data:
  81. if item != self._current_item:
  82. self._player.loadfile(item["path"], 1)
  83. self._playingPlaylist.append(item)
  84. else:
  85. break