mmdevice音频输出错误:无法初始化COM(错误0x80010106)

2021-12-01 10:57:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用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()

我在谷歌上搜索了一段时间,但仍然找不到解决方案。 我也看到了同样的问题 here,但没有人回答。 有人知道怎么修吗