Play mp3 using Python, PyQt, and Phonon

2024/10/7 20:27:44

I been trying all day to figure out the Qt's Phonon library with Python.

My long term goal is to see if I could get it to play a mms:// stream, but since I can't find an implementation of this done anywhere, I will figure that part out myself. (figured I'd put it out there if anyone knew more about this specifically, if not no big deal.)

Anyway, I figured I'd work backwards from a working example I found online. This launches a file browser and will play the mp3 file specified. I wanted to strip out the file browser stuff and get it down to the essentials of executing the script and having it play an Mp3 file with a hardcoded path.

I'm assuming my problem is a misunderstanding of setCurrentSource() and specifying the data types. (see: http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/phonon-mediasource.html#fileName)

I'm keeping my question kind of broad because ANY help with understanding Phonon would be greatly appreciated.

import sysfrom PyQt4.QtGui import QApplication, QMainWindow, QDirModel, QColumnView
from PyQt4.QtGui import QFrame
from PyQt4.QtCore import SIGNAL
from PyQt4.phonon import Phononclass MainWindow(QMainWindow):m_model = QDirModel()def __init__(self):QMainWindow.__init__(self)self.m_fileView = QColumnView(self)self.m_media = Noneself.setCentralWidget(self.m_fileView)self.m_fileView.setModel(self.m_model)self.m_fileView.setFrameStyle(QFrame.NoFrame)self.connect(self.m_fileView,SIGNAL("updatePreviewWidget(const QModelIndex &)"), self.play)def play(self, index):self.delayedInit()self.m_media.setCurrentSource(Phonon.MediaSource(self.m_model.filePath(index)))self.m_media.play()def delayedInit(self):if not self.m_media:self.m_media = Phonon.MediaObject(self)audioOutput = Phonon.AudioOutput(Phonon.MusicCategory, self)Phonon.createPath(self.m_media, audioOutput)def main():app = QApplication(sys.argv)QApplication.setApplicationName("Phonon Tutorial 2 (Python)")mw = MainWindow()mw.show()sys.exit(app.exec_())if __name__ == '__main__':main()
Answer

Phonon supports different audio file formats on different platforms, using the system's own support for media formats, so it could be that your system doesn't provide libraries for playing MP3 files. Certainly, MP3 is not supported out of the box on some Linux distributions. If you are using Linux, please take a look at the following page for information about enabling MP3 support:

http://doc.qt.io/qt-4.8/phonon-overview.html#linux

Another way to diagnose problems with Phonon's media formats is to run the Capabilities example provided with Qt:

http://doc.qt.io/qt-4.8///qt-phonon-capabilities-example.html

This should tell you which media formats are supported by your system.

https://en.xdnf.cn/q/70203.html

Related Q&A

Python dictionary keys(which are class objects) comparison with multiple comparer

I am using custom objects as keys in python dictionary. These objects has some default hash and eq methods defined which are being used in default comparison But in some function i need to use a diffe…

How can I make np.save work for an ndarray subclass?

I want to be able to save my array subclass to a npy file, and recover the result later.Something like:>>> class MyArray(np.ndarray): pass >>> data = MyArray(np.arange(10)) >>&g…

With ResNet50 the validation accuracy and loss is not changing

I am trying to do image recognition with ResNet50 in Python (keras). I tried to do the same task with VGG16, and I got some results like these (which seem okay to me): resultsVGG16 . The training and v…

string has incorrect type (expected str, got spacy.tokens.doc.Doc)

I have a dataframe:train_review = train[review] train_reviewIt looks like:0 With all this stuff going down at the moment w... 1 \The Classic War of the Worlds\" by Timothy Hi... 2 T…

custom URLs using django rest framework

I am trying to use the django rest framework to expose my models as APIs.serializersclass UserSerializer(serializers.HyperlinkedModelSerializer):class Meta:model = Userviewsetclass UserViewSet(viewsets…

Does python logging.FileHandler use block buffering by default?

The logging handler classes have a flush() method. And looking at the code, logging.FileHandler does not pass a specific buffering mode when calling open(). Therefore when you write to a log file, it …

Non brute force solution to Project Euler problem 25

Project Euler problem 25:The Fibonacci sequence is defined by the recurrence relation: Fn = Fn−1 + Fn−2, where F1 = 1 and F2 = 1. Hence the first 12 terms will be F1 = 1, F2 = 1, F3 = 2, F4 = 3, F5 =…

python:class attribute/variable inheritance with polymorphism?

In my endeavours as a python-apprentice i got recently stuck at some odd (from my point of view) behaviour if i tried to work with class attributes. Im not complaining, but would appreciate some helpfu…

Unable to load firefox in selenium webdriver in python

I have installed Python 3.6.2, Selenium 3.5.0 with GeckoDriver 0.18.0 and the firefox version is 54.0.1version on windows 7. I am trying to run a selenium script which is loading a firefox where i get …

Plot hyperplane Linear SVM python

I am trying to plot the hyperplane for the model I trained with LinearSVC and sklearn. Note that I am working with natural languages; before fitting the model I extracted features with CountVectorizer …