PyQt5 QtMultimedia

1.8k Views Asked by At

So I'm trying to create a GUI that searches a dictionary for a word, returns the entries and suggestions and downloads the audio, returning "related words" (sometimes more than one) that have audio. Then, the user clicks on one of three buttons to play the audio, based on the dialect. I can get the words to play, but I can't get the last played one to close so I can download another.

Here is the relevant code:

@staticmethod
    def munster():
            """ This method plays the Munster recording, if it exists"""
            url = QtCore.QUrl.fromLocalFile("./CanM.mp3")
            content = QtMultimedia.QMediaContent(url)
            player = QtMultimedia.QMediaPlayer()
            player.setMedia(content)
            player.play()
            player.stateChanged(app.quit)

When it gets to the last line it gives me this error:

TypeError: native Qt signal is not callable

Is there any way to close the player to allow downloading of another audio file with the same name?

For the record, I'm using the latest release of PyQt5, and Qt5.4 on Python 3.4 with Windows 81. However, I'm also working on this project (with the same Qt and PyQt) on an Archlinux system as well, and would like it to be portable easily.

Edit: Quite easy, once I started looking at what all was contained within the Player class. Just needed to use the disconnect method.

Edit2: Edited to show complete working code

 @staticmethod
    def play_audio(dialect):
        file_names = {'Munster': './CanM.mp3', 'Connacht': './CanC.mp3', 'Ulster': './CanU.mp3'}
        url = QtCore.QUrl.fromLocalFile(os.path.abspath(file_names[dialect]))
        content = QtMultimedia.QMediaContent(url)
        player = QtMultimedia.QMediaPlayer()
        player.setMedia(content)
        player.play()
        player.stateChanged.connect(lambda: player.disconnect())
0

There are 0 best solutions below