Implement volume control

This commit is contained in:
Wiktor Zykubek 2023-06-14 16:22:48 +02:00
parent de72aad958
commit 08ae286797
No known key found for this signature in database
GPG Key ID: 0DAA9DC01449FCA2
2 changed files with 21 additions and 3 deletions

View File

@ -107,7 +107,7 @@
</widget>
</item>
<item alignment="Qt::AlignRight">
<widget class="QSlider" name="horizontalSlider_2">
<widget class="QSlider" name="volumeHorizontalSlider">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Fixed">
<horstretch>0</horstretch>
@ -128,7 +128,8 @@
<string>...</string>
</property>
<property name="icon">
<iconset theme="audio-volume-medium"/>
<iconset theme="audio-volume-medium">
<normaloff>.</normaloff>.</iconset>
</property>
</widget>
</item>

View File

@ -24,11 +24,13 @@ class MainWindow(QMainWindow):
self.__player = QMediaPlayer()
self.__audio = QAudioOutput()
self.__player.setAudioOutput(self.__audio)
self.__audio.setVolume(self.ui.horizontalSlider_2.value())
self.setVolume()
self.getGroups()
self.ui.groupslistWidget.itemClicked.connect(self.getStations)
self.ui.stationslistWidget.itemClicked.connect(self.playRadio)
self.ui.toolButton.clicked.connect(self.togglePlayer)
self.ui.volumeHorizontalSlider.valueChanged.connect(self.setVolume)
self.ui.volumeToolButton.clicked.connect(self.toggleMute)
def getGroups(self):
for el in self.stations_slug["groups"]:
@ -58,6 +60,21 @@ class MainWindow(QMainWindow):
else:
return json.loads(resp.text)
def setVolume(self, volume: int = None):
if volume is None:
volume = self.ui.volumeHorizontalSlider.value()
self.__audio.setVolume(volume / 100)
def toggleMute(self):
if self.ui.volumeHorizontalSlider.value() == 0:
self.ui.volumeHorizontalSlider.setValue(70)
self.ui.volumeToolButton.setIcon(QIcon.fromTheme("audio-volume-medium"))
self.setVolume(70)
else:
self.ui.volumeHorizontalSlider.setValue(0)
self.ui.volumeToolButton.setIcon(QIcon.fromTheme("audio-volume-muted"))
self.setVolume(0)
def playRadio(self):
station = self.ui.stationslistWidget.selectedItems()[0].text()
stream_url = None