I am trying to use PyQt4 and Phonon (on windows 8 64 bits) to play a song from the internet (streaming, ex: http://dr5huvbk6x9di.cloudfront.net/cloudfront_songs/file4.ogg)
To play a song from the filesystem is working, but when I try to play from the internet it doesn't. I read the documentation and it seems everything is fine. The error is a FatalError, so is hard to understand what is going on. Phonon can't play the song from the internet?
Another questions is that I read that phonon has been deprecated and we have PyQt5. So, which is the best way to do what I am trying to do.
Here is my code. It is a little bit messy because I just wanted to work, so I could understand and then make it better. Thank you
#!/usr/bin/env python
import sys
from PyQt4 import QtGui
from PyQt4 import QtCore
from PyQt4.phonon import Phonon
class MainWindow(QtGui.QMainWindow):
def __init__(self, win_parent=None):
QtGui.QMainWindow.__init__(self, win_parent)
self.create_widgets()
def create_widgets(self):
# Widgets
self.label = QtGui.QLabel("ply music player")
self.fs_button = QtGui.QPushButton("FileSystem", self)
self.ws_button = QtGui.QPushButton("WebStream", self)
# Phonon actions
self.mediaObject = Phonon.MediaObject(self)
self.audioOutput = Phonon.AudioOutput(Phonon.MusicCategory, self)
Phonon.createPath(self.mediaObject, self.audioOutput)
# Connect signal
self.fs_button.clicked.connect(self.on_fs_clicked)
self.mediaObject.stateChanged.connect(self.handleStateChanged)
self.ws_button.clicked.connect(self.on_ws_clicked)
# Vertical layout (manages the layout automatically)
v_box = QtGui.QVBoxLayout()
v_box.addWidget(self.fs_button)
v_box.addWidget(self.ws_button)
# Create central widget, add layout and set
central_widget = QtGui.QWidget()
central_widget.setLayout(v_box)
self.setCentralWidget(central_widget)
def on_fs_clicked(self):
if self.mediaObject.state() == Phonon.PlayingState:
self.mediaObject.stop()
else:
files = QtGui.QFileDialog.getOpenFileNames(self, self.fs_button.text())
if files:
songs = []
for file in files:
songs.append(Phonon.MediaSource(file))
self.mediaObject.setQueue(songs)
self.mediaObject.play()
self.fs_button.setText("FileSystem")
def handleStateChanged(self, newstate, oldstate):
if newstate == Phonon.PlayingState:
self.fs_button.setText("Stop")
elif newstate == Phonon.StoppedState:
self.fs_button.setText("FileSystem")
elif newstate == Phonon.ErrorState:
source = self.mediaObject.currentSource().fileName()
print "ERROR: ", self.mediaObject.errorType()
print "ERROR: could not play:", source.toLocal8Bit().data()
def on_ws_clicked(self):
if self.mediaObject.state() == Phonon.PlayingState:
self.mediaObject.stop()
else:
song = "http://dr5huvbk6x9di.cloudfront.net/cloudfront_songs/file4.ogg"
self.mediaObject.setCurrentSource(Phonon.MediaSource(song))
print self.mediaObject.currentSource()
self.mediaObject.play()
self.ws_button.setText("WebStream")
if __name__ == "__main__":
ply = QtGui.QApplication(sys.argv)
ply.setApplicationName("Ply")
ply.setQuitOnLastWindowClosed(True)
main_window = MainWindow()
main_window.show()
sys.exit(ply.exec_())
The answer was installing codecs to play .ogg files. Thanks to @ekhumoro.