Create separate pause and stop button

This commit is contained in:
Wiktor Zykubek 2023-06-18 22:27:55 +02:00
parent 5358deab51
commit eeadc0a8c2
No known key found for this signature in database
GPG Key ID: 0DAA9DC01449FCA2
2 changed files with 32 additions and 8 deletions

View File

@ -147,6 +147,13 @@
</property> </property>
</widget> </widget>
</item> </item>
<item>
<widget class="QToolButton" name="stopToolButton">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item> <item>
<widget class="Line" name="line"> <widget class="Line" name="line">
<property name="lineWidth"> <property name="lineWidth">

View File

@ -24,8 +24,10 @@ class MainWindow(QMainWindow):
self.ui.setupUi(self) self.ui.setupUi(self)
volume_icon = self.style().standardIcon(QStyle.SP_MediaVolume) volume_icon = self.style().standardIcon(QStyle.SP_MediaVolume)
playback_icon = self.style().standardIcon(QStyle.SP_MediaPlay) playback_icon = self.style().standardIcon(QStyle.SP_MediaPlay)
stop_icon = self.style().standardIcon(QStyle.SP_MediaStop)
self.ui.volumeToolButton.setIcon(volume_icon) self.ui.volumeToolButton.setIcon(volume_icon)
self.ui.playbackToolButton.setIcon(playback_icon) self.ui.playbackToolButton.setIcon(playback_icon)
self.ui.stopToolButton.setIcon(stop_icon)
self.__stations = self.getData( self.__stations = self.getData(
"https://open.fm/radio/api/v2/ofm/stations_slug.json" "https://open.fm/radio/api/v2/ofm/stations_slug.json"
@ -45,6 +47,7 @@ class MainWindow(QMainWindow):
self.ui.podcastListWidget.itemClicked.connect(self.printPodcastEpisodes) self.ui.podcastListWidget.itemClicked.connect(self.printPodcastEpisodes)
self.ui.stationsListWidget.itemClicked.connect(self.playRadio) self.ui.stationsListWidget.itemClicked.connect(self.playRadio)
self.ui.playbackToolButton.clicked.connect(self.togglePlayer) self.ui.playbackToolButton.clicked.connect(self.togglePlayer)
self.ui.stopToolButton.clicked.connect(self.stopPlayer)
self.ui.volumeHorizontalSlider.valueChanged.connect(self.setVolume) self.ui.volumeHorizontalSlider.valueChanged.connect(self.setVolume)
self.ui.volumeToolButton.clicked.connect(self.toggleMute) self.ui.volumeToolButton.clicked.connect(self.toggleMute)
@ -159,23 +162,37 @@ class MainWindow(QMainWindow):
stream_url = e["file"].replace("https", "http") stream_url = e["file"].replace("https", "http")
self.__player.setSource(QUrl(stream_url)) self.__player.setSource(QUrl(stream_url))
self.__fix_playback()
# Required to avoid crashing. For some reason if you want to change def __fix_playback(self) -> None:
# the station for the first time, you need to stop and resume playback. """Fix playack."""
# For some reason if you want to change the station for the first time,
# you need to stop and resume playback.
# If you won't, application would crash. # If you won't, application would crash.
for _ in range(3): self.__player.play()
self.togglePlayer() self.__player.stop()
self.__player.play()
icon = self.style().standardIcon(QStyle.SP_MediaPause)
self.ui.playbackToolButton.setIcon(icon)
def togglePlayer(self) -> None: def togglePlayer(self) -> None:
"""Toggle playback (play/stop).""" """Toggle playback (play/pause)."""
pb_state = QMediaPlayer.PlaybackState pb_state = QMediaPlayer.PlaybackState
if self.__player.playbackState() == pb_state.PlayingState: if self.__player.playbackState() == pb_state.PlayingState:
self.__player.stop() self.__player.pause()
icon = self.style().standardIcon(QStyle.SP_MediaPlay) icon = self.style().standardIcon(QStyle.SP_MediaPlay)
elif self.__player.playbackState() == pb_state.StoppedState: elif self.__player.playbackState() in [
pb_state.PausedState,
pb_state.StoppedState
]:
self.__player.play() self.__player.play()
icon = self.style().standardIcon(QStyle.SP_MediaStop) icon = self.style().standardIcon(QStyle.SP_MediaPause)
else: else:
pass pass
self.ui.playbackToolButton.setIcon(icon) self.ui.playbackToolButton.setIcon(icon)
def stopPlayer(self) -> None:
self.__player.stop()
icon = self.style().standardIcon(QStyle.SP_MediaPlay)
self.ui.playbackToolButton.setIcon(icon)