PyQt6 如何使用滑块设置音量

问题描述 投票:0回答:1

我无法使用滑块更改播放音乐的音量。

我尝试使用audio_output更改音量,但不起作用。

import sys
from PyQt6.QtCore import Qt, QUrl
from PyQt6.QtMultimedia import QMediaPlayer, QAudioOutput
from PyQt6.QtWidgets import QApplication, QWidget, QPushButton, QSlider, QVBoxLayout, QHBoxLayout, QLabel


class MusicPlayer(QWidget):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("Музыкальный плеер")
        self.setGeometry(100, 100, 400, 200)

        self.initUI()
        self.initPlayer()

    def initPlayer(self):
        self.player = QMediaPlayer()
        self.audio_output = QAudioOutput()
        self.player.setAudioOutput(self.audio_output)
        file_name = "song.mp3"
        self.player.setSource(QUrl.fromLocalFile(file_name))
        self.audio_output.setVolume(50)
        self.player.play()

    def initUI(self):


        self.play_button = QPushButton("Play")
        self.play_button.clicked.connect(self.play_music)

        self.pause_button = QPushButton("Pause")
        self.pause_button.clicked.connect(self.pause_music)

        self.stop_button = QPushButton("Stop")
        self.stop_button.clicked.connect(self.stop_music)

        self.volume_slider = QSlider(Qt.Orientation.Horizontal)
        self.volume_slider.setValue(50)
        self.volume_slider.setMaximum(100)
        self.volume_slider.setToolTip("Volume")
        self.volume_slider.valueChanged.connect(self.set_volume)

        self.track_label = QLabel("Название трека")

        vbox = QVBoxLayout()
        hbox = QHBoxLayout()

        hbox.addWidget(self.play_button)
        hbox.addWidget(self.pause_button)
        hbox.addWidget(self.stop_button)
        hbox.addWidget(self.volume_slider)

        vbox.addWidget(self.track_label)
        vbox.addLayout(hbox)

        self.setLayout(vbox)
        
    def play_music(self):
        self.player.play()

    def pause_music(self):
        self.player.pause()

    def stop_music(self):
        self.player.stop()

    def set_volume(self):
        self.volume = self.volume_slider.value()
        print(self.volume)
        print(type(self.volume))
        self.audio_output.setVolume(self.volume)


app = QApplication(sys.argv)
playerM = MusicPlayer()
playerM.show()
sys.exit(app.exec())
python pyqt pyqt6 qmediaplayer qslider
1个回答
0
投票

抱歉,我有 PyQt5
设置您的导入并尝试:

import sys
'''
from PyQt6.QtCore import Qt, QUrl
from PyQt6.QtMultimedia import QMediaPlayer, QAudioOutput
from PyQt6.QtWidgets import QApplication, QWidget, QPushButton, QSlider, QVBoxLayout, QHBoxLayout, QLabel
'''
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtMultimedia import QMediaPlayer, QMediaContent
from PyQt5.Qt import *


class MusicPlayer(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Музыкальный плеер")
        self.setGeometry(100, 100, 400, 200)

        self.initUI()
        self.initPlayer()

    def initPlayer(self):
        self.player = QMediaPlayer()
#        self.audio_output = QAudioOutput()
#        self.player.setAudioOutput(self.audio_output)
#        self.player.setSource(QUrl.fromLocalFile(file_name))
#        self.audio_output.setVolume(50)

# +++ vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
        file_name = "sounds/zvuki_prirody.mp3"   # "song.mp3" eустановите свое 
        file = QUrl.fromLocalFile(file_name)
        self.content = QMediaContent(file)
        self.player.setMedia(self.content)
        self.player.setVolume(50.0)
# +++ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        self.player.play()

    def initUI(self):
        self.play_button = QPushButton("Play")
        self.play_button.clicked.connect(self.play_music)

        self.pause_button = QPushButton("Pause")
        self.pause_button.clicked.connect(self.pause_music)

        self.stop_button = QPushButton("Stop")
        self.stop_button.clicked.connect(self.stop_music)

        self.volume_slider = QSlider(Qt.Orientation.Horizontal)
        self.volume_slider.setValue(50)
        self.volume_slider.setMaximum(100)
        self.volume_slider.setToolTip("Volume")
        self.volume_slider.valueChanged.connect(self.set_volume)

        self.track_label = QLabel("Название трека")

        vbox = QVBoxLayout()
        hbox = QHBoxLayout()

        hbox.addWidget(self.play_button)
        hbox.addWidget(self.pause_button)
        hbox.addWidget(self.stop_button)
        hbox.addWidget(self.volume_slider)

        vbox.addWidget(self.track_label)
        vbox.addLayout(hbox)

        self.setLayout(vbox)
        
    def play_music(self):
        self.player.play()

    def pause_music(self):
        self.player.pause()

    def stop_music(self):
        self.player.stop()

# ---------------------> vvvvvv <----------------------------------------------
    def set_volume(self, volume):
#        self.volume = self.volume_slider.value()
#        self.audio_output.setVolume(self.volume)

        self.player.setVolume(volume)                              # +++ volume


if __name__ == "__main__":
    app = QApplication(sys.argv)
    playerM = MusicPlayer()
    playerM.show()
    sys.exit(app.exec())

© www.soinside.com 2019 - 2024. All rights reserved.