-1

I have my mp3 files, and about to run it from designed window I made from Qt Designer (using PyQt 4 and Python 2.7)

I do not know how to play it only from button without those mp3 player alike, I have found many projects that also can play mp3 files but it's a mp3 player not like this, only buttons to preview a mp3 files that have length only 12 seconds.

here is the code of the designed window, there are 4 buttons for each of mp3 files that I have

#PyQt4 UI code generator 4.10
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_strsetting(object):
    def setupUi(self, strsetting):
        strsetting.setObjectName(_fromUtf8("strsetting"))
        strsetting.resize(490, 288)
        strsetting.setMinimumSize(QtCore.QSize(490, 288))
        strsetting.setMaximumSize(QtCore.QSize(490, 288))
        self.centralwidget = QtGui.QWidget(strsetting)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.frame = QtGui.QFrame(self.centralwidget)
        self.frame.setGeometry(QtCore.QRect(0, 0, 491, 291))
        self.frame.setFrameShape(QtGui.QFrame.StyledPanel)
        self.frame.setFrameShadow(QtGui.QFrame.Raised)
        self.frame.setObjectName(_fromUtf8("frame"))
        self.verticalLayoutWidget = QtGui.QWidget(self.frame)
        self.verticalLayoutWidget.setGeometry(QtCore.QRect(20, 10, 160, 261))
        self.verticalLayoutWidget.setObjectName(_fromUtf8("verticalLayoutWidget"))
        self.verticalLayout = QtGui.QVBoxLayout(self.verticalLayoutWidget)
        self.verticalLayout.setMargin(0)
        self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
        self.pushButton_2 = QtGui.QPushButton(self.verticalLayoutWidget)
        self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
        self.verticalLayout.addWidget(self.pushButton_2)
        self.pushButton_4 = QtGui.QPushButton(self.verticalLayoutWidget)
        self.pushButton_4.setObjectName(_fromUtf8("pushButton_4"))
        self.verticalLayout.addWidget(self.pushButton_4)
        self.pushButton_3 = QtGui.QPushButton(self.verticalLayoutWidget)
        self.pushButton_3.setObjectName(_fromUtf8("pushButton_3"))
        self.verticalLayout.addWidget(self.pushButton_3)
        self.pushButton = QtGui.QPushButton(self.verticalLayoutWidget)
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.verticalLayout.addWidget(self.pushButton)
        self.label = QtGui.QLabel(self.frame)
        self.label.setGeometry(QtCore.QRect(280, 30, 91, 16))
        self.label.setObjectName(_fromUtf8("label"))
        self.graphicsView = QtGui.QGraphicsView(self.frame)
        self.graphicsView.setGeometry(QtCore.QRect(210, 70, 256, 192))
        self.graphicsView.setObjectName(_fromUtf8("graphicsView"))
        strsetting.setCentralWidget(self.centralwidget)

    self.retranslateUi(strsetting)
    QtCore.QMetaObject.connectSlotsByName(strsetting)

def retranslateUi(self, strsetting):
    strsetting.setWindowTitle(_translate("strsetting", "MainWindow", None))
    self.pushButton_2.setText(_translate("strsetting", "PushButton", None))
    self.pushButton_4.setText(_translate("strsetting", "PushButton", None))
    self.pushButton_3.setText(_translate("strsetting", "PushButton", None))
    self.pushButton.setText(_translate("strsetting", "PushButton", None))
    self.label.setText(_translate("strsetting", "TextLabel", None))
Mekha
  • 1
  • 4
  • Possible duplicate of [Playing sound with Pyqt4 and QSound](https://stackoverflow.com/questions/15750865/playing-sound-with-pyqt4-and-qsound) – eyllanesc Jul 07 '18 at 05:19
  • @eyllanesc QSound not supported on my version, but I have a solution so it can played but not through the button tho – Mekha Jul 07 '18 at 05:25
  • import playsound playsound.playsound('equal1.mp3', True) – Mekha Jul 07 '18 at 05:25
  • Why do not I mention it? – eyllanesc Jul 07 '18 at 05:26
  • @eyllanesc a little bit workaround and I nailed the answer's, change the QSound into playsound and DONE! Thank you so much for helping me review my own question – Mekha Jul 07 '18 at 05:30
  • @Mekha you should consider answering your own question on here if you have come to a good conclusion. This could be helpful for any future developers that have a similar question. – MillerMedia Aug 04 '18 at 00:17
  • 1
    @MillerMedia ah my apologize, I only put my answer on my question's post before. Now I added the answer on the answer post – Mekha Aug 08 '18 at 03:50

1 Answers1

0

Got myself an answer, and also thanks to eyllanesc for helping me to review my question.

Maybe QSound isn't supported in PyQt4 (in my version, no) but there's a workaround how to played it using playsound instead. Here's example on using playsound :

import playsound

playsound.playsound('/path/to/filename.mp3', True)

I putting my mp3 files same as the .py files (The Python file) so it's kinda messy a bit, but you could also put your mp3 files in designed folder (just directed it). And if you still want to use QSound do as This Post did instead mine.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Mekha
  • 1
  • 4