scripts/mycroft/audio/services/mplayer/__init__.py
Go to the documentation of this file.
1 # Copyright 2017 Mycroft AI Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 #
15 from mycroft.audio.services import AudioBackend
16 from mycroft.util.log import LOG
17 try:
18  from py_mplayer import MplayerCtrl
19 except ImportError:
20  LOG.error("install py_mplayer with "
21  "pip install git+https://github.com/JarbasAl/py_mplayer")
22  raise
23 
24 
26  """
27  Audio backend for mplayer.
28  """
29 
30  def __init__(self, config, bus, name='mplayer'):
31  super(MPlayerService, self).__init__(config, bus)
32  self.config = config
33  self.bus = bus
34  self.name = name
35  self.index = 0
36  self.normal_volume = None
37  self.tracks = []
38  self.mpc = MplayerCtrl()
39 
40  def supported_uris(self):
41  return ['file', 'http', 'https']
42 
43  def clear_list(self):
44  self.tracks = []
45 
46  def add_list(self, tracks):
47  self.tracks += tracks
48  LOG.info("Track list is " + str(tracks))
49 
50  def play(self, repeat=False):
51  """ Start playback of playlist.
52 
53  TODO: Add support for repeat
54  """
55  self.stop()
56  if len(self.tracks):
57  # play first track
58  self.mpc.loadfile(self.tracks[0])
59  # add other tracks
60  for track in self.tracks[1:]:
61  self.mpc.loadfile(track, 1)
62 
63  def stop(self):
64  self.mpc.stop()
65  return True # TODO: Return False if not playing
66 
67  def pause(self):
68  if not self.mpc.paused:
69  self.mpc.pause()
70 
71  def resume(self):
72  if self.mpc.paused:
73  self.mpc.pause()
74 
75  def next(self):
76  self.index = self.index + 1
77  if self.index > len(self.tracks):
78  self.index = 0
79  self.play()
80 
81  def previous(self):
82  self.index = self.index - 1
83  if self.index < 0:
84  self.index = 0
85  self.play()
86 
87  def lower_volume(self):
88  if self.normal_volume is None:
89  self.normal_volume = self.mpc.volume
90  self.mpc.volume = self.mpc.volume / 3
91 
92  def restore_volume(self):
93  if self.normal_volume:
94  self.mpc.volume = self.normal_volume
95  else:
96  self.mpc.volume = 50
97  self.normal_volume = None
98 
99  def track_info(self):
100  """
101  Fetch info about current playing track.
102 
103  Returns:
104  Dict with track info.
105  """
106  ret = {}
107  ret['title'] = self.mpc.get_meta_title()
108  ret['artist'] = self.mpc.get_meta_artist()
109  ret['album'] = self.mpc.get_meta_album()
110  ret['genre'] = self.mpc.get_meta_genre()
111  ret['year'] = self.mpc.get_meta_year()
112  ret['track'] = self.mpc.get_meta_track()
113  ret['comment'] = self.mpc.get_meta_comment()
114  return ret
115 
116  def shutdown(self):
117  """
118  Shutdown mplayer
119 
120  """
121  self.mpc.destroy()
122 
123 
124 def load_service(base_config, emitter):
125  backends = base_config.get('backends', [])
126  services = [(b, backends[b]) for b in backends
127  if backends[b]['type'] == 'mplayer' and
128  backends[b].get("active", True)]
129  instances = [MPlayerService(s[1], emitter, s[0]) for s in services]
130  return instances
def get(phrase, lang=None, context=None)


mycroft_ros
Author(s):
autogenerated on Mon Apr 26 2021 02:35:40