我试图使用 Pafy、vlc 和 PyQt6 制作一个音频播放器。 当我运行下面的代码时,它运行得非常完美。
import pafy,vlc
url="https://www.youtube.com/watch?v=6Duo89XuYIM&list=RDMMIoCoIxkGkVw&index=20"
video=pafy.new(url)
best = video.getbestaudio()
playurl = best.url
Instance = vlc.Instance()
player = Instance.media_player_new()
Media = Instance.media_new(playurl)
Media.get_mrl()
player.set_media(Media)
player.play()
但是,当我将其重写为一个类并从我的 PyQt6 代码调用时,我收到了错误消息:
mmdevice audio output error: cannot initialize COM (error 0x80010106)
这是我的 GUI 代码:
import sys
import play_song
from PyQt6.QtWidgets import *
from PyQt6.QtGui import QFont
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
QToolTip.setFont(QFont('SansSerif', 10))
player=play_song.play_song()
play = QPushButton('play', self)
play.setToolTip('This is a <b>QPushButton</b> widget')
play.resize(play.sizeHint())
play.clicked.connect(player.play)
play.move(50, 50)
pause = QPushButton('pause', self)
pause.setToolTip('This is a <b>QPushButton</b> widget')
pause.resize(pause.sizeHint())
pause.clicked.connect(player.pause)
pause.move(150, 50)
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('Tooltips')
self.show()
def main():
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec())
if __name__ == '__main__':
main()
这是 play_song 类:
import pafy,vlc
import youtube_dl
import keyboard
class play_song():
def __init__(self):
self.url="https://www.youtube.com/watch?v=6Duo89XuYIM&list=RDMMIoCoIxkGkVw&index=20"
self.video=pafy.new(self.url)
self.best = self.video.getbestaudio()
self.playurl = self.best.url
self.Instance = vlc.Instance()
self.player = self.Instance.media_player_new()
self.Media = self.Instance.media_new(self.playurl)
self.Media.get_mrl()
self.Media.add_option('start-time=120.0')
self.Media.add_option('stop-time=130.0')
self.player.set_media(self.Media)
def play(self):
self.player.play()
def pause(self):
self.player.pause()
def stop(self):
self.player.stop()
经过一番尝试,发现错误来自于这两行
self.Instance = vlc.Instance()
self.player = self.Instance.media_player_new()
我在Google上搜索了一段时间,但仍然找不到解决方案。 我看到了同样的问题 这里,但没有人回答。 有谁知道如何解决吗?
Я знаю уже поздно наверное)))... Но попробуйте просто включить звук на вашем устройстве。 У меня сработало