使用 QAudioOutput 和 QMediaPlayer (PyQt6) 的淡化效果
Posted
技术标签:
【中文标题】使用 QAudioOutput 和 QMediaPlayer (PyQt6) 的淡化效果【英文标题】:Fading effect with QAudioOutput and QMediaPlayer (PyQt6) 【发布时间】:2022-01-09 05:19:53 【问题描述】:我正在开发一个音乐播放器应用程序,但我遇到了一个我不知道如何解决的问题。我希望音频在启动或取消暂停时以给定的间隔(例如 0.6 秒)淡入,并在暂停时以相同的间隔淡出。我尝试使用 QThread 来保持我的 ui 响应,同时使用 for 循环逐渐降低音量(迭代 10 次,每次降低音量并睡眠一定时间,0.6 / 10 秒)。但它不起作用,按钮仍然不可点击,只有在淡入或淡出后才会更新。如果有人设法解决了这样的问题,请提供帮助,将不胜感激!下面是在pyqt中播放音频的简单代码:
import sys
from PyQt6 import QtWidgets
from PyQt6.QtCore import QUrl
from PyQt6.QtMultimedia import QMediaPlayer, QAudioOutput
from PyQt6.QtWidgets import QWidget, QVBoxLayout, QPushButton, QStyle
class MainWindowUi(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self._setup_ui()
self.setWindowTitle('sound fading')
self.setGeometry(100, 100, 100, 100)
def _setup_ui(self):
self.central_widget = QWidget(self)
self.central_widget_layout = QVBoxLayout(self.central_widget)
self.setCentralWidget(self.central_widget)
self.player = AudioPlayer()
self.user_action = -1 # 0 - stopped, 1 - playing, 2 - paused
self.play_button = QPushButton()
self.play_button.clicked.connect(self.play_pause_button_clicked)
self.play_icon = self.style().standardIcon(QStyle.StandardPixmap.SP_MediaPlay)
self.pause_icon = self.style().standardIcon(QStyle.StandardPixmap.SP_MediaPause)
self.play_button.setIcon(self.play_icon)
self.central_widget_layout.addWidget(self.play_button)
def play(self):
print("Play")
self.play_button.setIcon(self.pause_icon)
self.user_action = 1
self.player.setSource(QUrl("Some audio file path goes here."))
self.player.play()
def pause(self):
print("Pause")
self.play_button.setIcon(self.play_icon)
self.user_action = 2
self.player.pause()
def unpause(self):
print("Unpause")
self.play_button.setIcon(self.pause_icon)
self.user_action = 1
self.player.play()
def play_pause_button_clicked(self):
if self.user_action <= 0:
self.play()
elif self.user_action == 1:
self.pause()
elif self.user_action == 2:
self.unpause()
class AudioPlayer(QMediaPlayer):
def __init__(self, parent=None):
super().__init__(parent)
self.audio_output = QAudioOutput()
self.setAudioOutput(self.audio_output)
self.audioOutput().setVolume(0.3)
self.current_volume = self.audio_output.volume()
def play(self):
super().play()
def pause(self):
super().pause()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
mainWindow = MainWindowUi()
mainWindow.show()
sys.exit(app.exec())
【问题讨论】:
听起来您正在寻找类似QVariantAnimation
的东西(链接是PySide2
——由于某种原因找不到相关的PyQt6
文档。)
是的!非常感谢,我在音频输出音量上使用了 QPropertyAnimation,效果很好!
@N1teShad0w 我建议您使用minimal reproducible example 发布您自己的答案,以展示您是如何解决问题的,因为它可能对其他人也有用。
【参考方案1】:
所以我让它与 QPropertyAnimation 一起工作!仍在处理关键值和缓动曲线,但确实更接近我想要的结果。
import sys
from PyQt6 import QtWidgets
from PyQt6.QtCore import QUrl, QPropertyAnimation, QEasingCurve
from PyQt6.QtMultimedia import QMediaPlayer, QAudioOutput
from PyQt6.QtWidgets import QWidget, QVBoxLayout, QPushButton, QStyle
from audio_player import AudioPlayer
class MainWindowUi(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self._setup_ui()
self.setWindowTitle('sound fading')
self.setGeometry(100, 100, 100, 100)
def _setup_ui(self):
self.central_widget = QWidget(self)
self.central_widget_layout = QVBoxLayout(self.central_widget)
self.setCentralWidget(self.central_widget)
self.player = AudioPlayer()
self.user_action = -1 # 0 - stopped, 1 - playing, 2 - paused
self.play_button = QPushButton()
self.play_button.clicked.connect(self.play_pause_button_clicked)
self.play_icon = self.style().standardIcon(QStyle.StandardPixmap.SP_MediaPlay)
self.pause_icon = self.style().standardIcon(QStyle.StandardPixmap.SP_MediaPause)
self.play_button.setIcon(self.play_icon)
self.central_widget_layout.addWidget(self.play_button)
def play(self):
print("Play")
self.play_button.setIcon(self.pause_icon)
self.user_action = 1
self.player.setSource(QUrl("some audio file path"))
self.player.play()
def pause(self):
print("Pause")
self.play_button.setIcon(self.play_icon)
self.user_action = 2
self.player.pause()
def unpause(self):
print("Unpause")
self.play_button.setIcon(self.pause_icon)
self.user_action = 1
self.player.play()
def play_pause_button_clicked(self):
if self.user_action <= 0:
self.play()
elif self.user_action == 1:
self.pause()
elif self.user_action == 2:
self.unpause()
class AudioPlayer(QMediaPlayer):
def __init__(self, parent=None):
super().__init__(parent)
self.audio_output = QAudioOutput()
self.audio_output.volumeChanged.connect(lambda: print(self.audio_output.volume()))
self.setAudioOutput(self.audio_output)
self.audioOutput().setVolume(.5)
self.current_volume = self.audio_output.volume()
self.fade_in_anim = QPropertyAnimation(self.audio_output, b"volume")
self.fade_in_anim.setDuration(1400)
self.fade_in_anim.setStartValue(0.01)
self.fade_in_anim.setEndValue(self.current_volume)
self.fade_in_anim.setEasingCurve(QEasingCurve.Type.Linear)
self.fade_in_anim.setKeyValueAt(0.01, 0.01)
self.fade_out_anim = QPropertyAnimation(self.audio_output, b"volume")
self.fade_out_anim.setDuration(600)
self.fade_out_anim.setStartValue(self.current_volume)
self.fade_out_anim.setEndValue(0)
self.fade_out_anim.setEasingCurve(QEasingCurve.Type.Linear)
self.fade_out_anim.setKeyValueAt(0.01, self.current_volume)
self.fade_out_anim.finished.connect(super().pause)
def play(self):
self.audio_output.setVolume(0.01)
self.fade_in_anim.setEndValue(self.current_volume)
super().play()
self.fade_in_anim.start()
def pause(self):
self.current_volume = self.audio_output.volume()
self.fade_out_anim.setStartValue(self.current_volume)
self.fade_out_anim.start()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
mainWindow = MainWindowUi()
mainWindow.show()
sys.exit(app.exec())
【讨论】:
以上是关于使用 QAudioOutput 和 QMediaPlayer (PyQt6) 的淡化效果的主要内容,如果未能解决你的问题,请参考以下文章
通过 QAudioOutput 播放 wav 文件时只有嗡嗡声
用 Qt 的 QAudioOutput 类播放 WAV 音频文件(使用了libsndfile外部库)