diff --git a/SpotifyRecorder.py b/SpotifyRecorder.py index 9aab8d6db07b4ed57a1f119dbdad302bcae844e9..899a31cc8fdeafe407f8485253ff61ae599ea411 100644 --- a/SpotifyRecorder.py +++ b/SpotifyRecorder.py @@ -111,7 +111,7 @@ class SpotifyRecorder: recorder = SpotifyAudioRecorder(self._audioDeviceName, filePath) with recorder.record(): self.__play_track(deviceId, track['track']['uri']) - timeWaitedForPlaying = self.__wait_for_track_playing() + timeWaitedForPlaying = self.__wait_for_track_playing(track['track']['id']) self.__wait_for_track_end(track, timeWaitedForPlaying) recordedTracks.append(track['track']['name']) @@ -182,15 +182,19 @@ class SpotifyRecorder: def __play_track(self, deviceId: str, trackUri: str): self._spotify.start_playback(device_id=deviceId, uris=[trackUri]) - def __wait_for_track_playing(self) -> float: + def __wait_for_track_playing(self, expectedTrackId: str) -> float: LOGGER.debug(f'\t\tWait for track to start playing...') startTime = time.time() duration = 0 while time.time() - startTime < self._MAX_WAIT_TIME_TRACK_STARTING_IN_S: - if self._spotify.current_playback()['is_playing']: + currentPlayback = self._spotify.current_playback() + if currentPlayback['is_playing']: duration = time.time() - startTime - LOGGER.debug(f'\t\tTrack started playing after {duration:.1f}s') - break + if currentPlayback['item']['id'] == expectedTrackId: + LOGGER.debug(f'\t\tTrack started playing after {duration:.1f}s') + break + else: + raise RuntimeError('Wrong track started playing') time.sleep(1)