How can audio files be played in PyQt5?

To play audio files in PyQt5, you can use the QMediaPlayer class. Here is a simple example code:

from PyQt5.QtWidgets import QApplication, QPushButton
from PyQt5.QtMultimedia import QMediaPlayer, QMediaContent

app = QApplication([])

player = QMediaPlayer()

button = QPushButton("Play")


In this example, we start by importing the necessary modules and classes. Next, we create a QMediaPlayer object and use the setMedia() method to set the audio file to be played. Then, we create a button that, when clicked, calls the play() method of the player to play the audio file. Finally, we start the event loop of the application. You can adjust the file path and playback control logic as needed.

Leave a Reply 0

Your email address will not be published. Required fields are marked *