def set_volume(self, volume: float) -> None: """Set volume to a value between 0 and 1.""" old_volume = self._volume self._volume = volume # If we've got nothing we're supposed to be playing, # don't touch itunes/music. if self._current_playlist is None: return # If volume is going to zero, stop actually playing # but don't clear playlist. if old_volume > 0.0 and volume == 0.0: try: assert self._orig_volume is not None _ba.mac_music_app_stop() _ba.mac_music_app_set_volume(self._orig_volume) except Exception as exc: print('Error stopping iTunes music:', exc) elif self._volume > 0: # If volume was zero, store pre-playing volume and start # playing. if old_volume == 0.0: self._orig_volume = _ba.mac_music_app_get_volume() self._update_mac_music_app_volume() if old_volume == 0.0: self._play_current_playlist()
def _handle_play_command(self, target: Optional[str]) -> None: if target is None: if self._current_playlist is not None and self._volume > 0: try: assert self._orig_volume is not None _ba.mac_music_app_stop() _ba.mac_music_app_set_volume(self._orig_volume) except Exception as exc: print('Error stopping iTunes music:', exc) self._current_playlist = None else: # If we've got something playing with positive # volume, stop it. if self._current_playlist is not None and self._volume > 0: try: assert self._orig_volume is not None _ba.mac_music_app_stop() _ba.mac_music_app_set_volume(self._orig_volume) except Exception as exc: print('Error stopping iTunes music:', exc) # Set our playlist and play it if our volume is up. self._current_playlist = target if self._volume > 0: self._orig_volume = (_ba.mac_music_app_get_volume()) self._update_mac_music_app_volume() self._play_current_playlist()
def run(self) -> None: """Run the Music.app thread.""" from ba._general import Call from ba._lang import Lstr from ba._enums import TimeType _ba.set_thread_name("BA_MacMusicAppThread") _ba.mac_music_app_init() # Let's mention to the user we're launching Music.app in case # it causes any funny business (this used to background the app # sometimes, though I think that is fixed now) def do_print() -> None: _ba.timer(1.0, Call(_ba.screenmessage, Lstr(resource='usingItunesText'), (0, 1, 0)), timetype=TimeType.REAL) _ba.pushcall(do_print, from_other_thread=True) # Here we grab this to force the actual launch. _ba.mac_music_app_get_volume() _ba.mac_music_app_get_library_source() done = False while not done: self._commands_available.wait() self._commands_available.clear() # We're not protecting this list with a mutex but we're # just using it as a simple queue so it should be fine. while self._commands: cmd = self._commands.pop(0) if cmd[0] == 'DIE': self._handle_die_command() done = True break if cmd[0] == 'PLAY': self._handle_play_command(target=cmd[1]) elif cmd[0] == 'GET_PLAYLISTS': self._handle_get_playlists_command(target=cmd[1]) del cmd # Allows the command data/callback/etc to be freed.