From 7effe35e622b2ec10633f1e32418efa2022a62df Mon Sep 17 00:00:00 2001
From: Robert Goldmann <deadlocker@gmx.de>
Date: Sat, 13 Jan 2024 16:26:26 +0100
Subject: [PATCH] SpotifyRecorder: make sure no track is playing when starting
 to record next track

---
 SpotifyRecorder.py | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/SpotifyRecorder.py b/SpotifyRecorder.py
index cb83ee5..9374eb1 100644
--- a/SpotifyRecorder.py
+++ b/SpotifyRecorder.py
@@ -109,6 +109,8 @@ class SpotifyRecorder:
             LOGGER.info(
                 f'>>> Recording track {index + 1}/{len(tracks)}: #{indexInPlaylist} "{track["track"]["name"]}"...')
             try:
+                self.__stop_playback_if_playing(deviceId)
+
                 filePath = self.__determine_file_path(indexInPlaylist + 1, track)
                 recorder = SpotifyAudioRecorder(self._audioDeviceName, filePath)
                 with recorder.record():
@@ -194,6 +196,10 @@ class SpotifyRecorder:
     def __play_track(self, deviceId: str, trackUri: str) -> None:
         self._spotify.start_playback(device_id=deviceId, uris=[trackUri])
 
+    def __stop_playback_if_playing(self, deviceId: str) -> None:
+        if self._spotify.current_playback()['is_playing']:
+            self._spotify.pause_playback(device_id=deviceId)
+
     def __wait_for_track_playing(self, expectedTrackId: str) -> float:
         LOGGER.debug(f'\t\tWait for track to start playing...')
         startTime = time.time()
-- 
GitLab