2

I was trying to make a audio player using Pafy, vlc, and PyQt6. When I ran the code below, it worked perfect.

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()

However, when I rewrite it into a class and call from my PyQt6 code, I got the error message:

mmdevice audio output error: cannot initialize COM (error 0x80010106)

Here is my GUI code:

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()

And here is the play_song class:

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()

After some attempt, I found that the error came from these two lines

self.Instance = vlc.Instance()
self.player = self.Instance.media_player_new()

I've searched on Google for a while, but still cannot find a solution. I saw the same problem here, but no one answered it. Does anyone know how to fix it?

Adi_Hsiao_0410
  • 111
  • 2
  • 9
  • 1
    try change to `self.player=play_song.play_song()` and `pause.clicked.connect(self.player.pause)` – eyllanesc Jul 04 '21 at 14:37
  • @eyllanesc Thanks for your help. I just tried it, but still got the same error message. – Adi_Hsiao_0410 Jul 04 '21 at 14:42
  • 1
    Just out of curiosity, what happens if you put `p = play_song.play_song()` before `app = QApplication(sys.argv)`? – eyllanesc Jul 04 '21 at 14:57
  • Still got the message, but I found something cool. Despite that it showed that error, it in fact had loaded the audio well. After waiting for about 5 seconds after running the code , I pressed the play button and it played the audio well. (Still curious about why this error occurred) – Adi_Hsiao_0410 Jul 04 '21 at 15:04
  • [adding time.sleep helped me with the same problem](https://stackoverflow.com/questions/61704373/python-vlc-doesnt-play-audio-video-from-py-file/63103654#63103654) – vamsi miriyala Jul 05 '21 at 14:03
  • @vamsimiriyala Thanks for providing such solution. But in this case, I don't want to set the time in the beginning. I want to make it pause when I click on the "Pause" button. Does this still work? – Adi_Hsiao_0410 Jul 05 '21 at 14:27
  • Do you have an actual problem? Because some error logs can and should be ignored – mfkl Jul 06 '21 at 08:37

0 Answers0