Звук в Python и PyQT5

Когда запуская программу, нажимаю на клавиши производит один и тот же звук, как исправить?

Вот мой код:

import sys
from PyQt5 import QtMultimedia, QtCore
from PyQt5.QtWidgets import QWidget, QPushButton, QApplication

class Guitar(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setGeometry(250, 55, 1200, 800)
        self.setWindowTitle('Клавиши')

        self.First_button = QPushButton('Первая клавиша(клавиша 1)', self)
        self.First_button.resize(170, 50)
        self.First_button.move(40, 100)
        self.url = QtCore.QUrl.fromLocalFile('D:\\Downloads\\First_str.mp3')
        self.content = QtMultimedia.QMediaContent(self.url)
        self.player = QtMultimedia.QMediaPlayer()
        self.player.setMedia(self.content)
        self.First_button.clicked.connect(self.player.play)
        self.First_button.setShortcut('1')

        self.Second_button = QPushButton('Вторая клавиша(клавиша 2)', self)
        self.Second_button.resize(170, 50)
        self.Second_button.move(40, 200)
        self.url = QtCore.QUrl.fromLocalFile('D:\\Downloads\\Second_str.mp3')
        self.content = QtMultimedia.QMediaContent(self.url)
        self.player = QtMultimedia.QMediaPlayer()
        self.player.setMedia(self.content)
        self.Second_button.clicked.connect(self.player.play)
        self.Second_button.setShortcut('2')


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Guitar()
    ex.show()
    sys.exit(app.exec_())

Ответы (1 шт):

Автор решения: S. Nick

Попробуйте так:

import sys
from PyQt5 import QtMultimedia, QtCore
from PyQt5.QtWidgets import QWidget, QPushButton, QApplication

class Guitar(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setGeometry(250, 55, 1200, 800)
        self.setWindowTitle('Клавиши')

        self.first_button = QPushButton('Первая клавиша(клавиша 1)', self)
        self.first_button.resize(170, 50)
        self.first_button.move(40, 100)
        self.first_button.clicked.connect(lambda: self._play('bibi.mp3'))               # +++

        self.second_button = QPushButton('Вторая клавиша(клавиша 2)', self)
        self.second_button.resize(170, 50)
        self.second_button.move(40, 200)
        self.second_button.clicked.connect(lambda: self._play('D:/_Qt/Mp3/alarm.mp3'))   # +++
        
    def _play(self, url):                                                                # +++
        self.url = QtCore.QUrl.fromLocalFile(url) #'D:\\Downloads\\Second_str.mp3')
        self.content = QtMultimedia.QMediaContent(self.url)
        self.player = QtMultimedia.QMediaPlayer()
        self.player.setMedia(self.content) 
        self.player.play()        


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Guitar()
    ex.show()
    sys.exit(app.exec_())

введите сюда описание изображения

→ Ссылка