繁体   English   中英

如何使用python从互联网播放歌曲

[英]How to play a song from the internet using python

我正在尝试使用PyQt4和Phonon(在Windows 8 64位上)播放来自互联网的歌曲(流式传输,例如: http : //dr5huvbk6x9di.cloudfront.net/cloudfront_songs/file4.ogg

从文件系统播放歌曲是可行的,但是当我尝试从Internet播放时却没有。 我阅读了文档,似乎一切都很好。 该错误是FatalError,因此很难理解发生了什么。 声子无法从网络播放歌曲?

另一个问题是,我读到声子已被弃用,而我们拥有PyQt5。 所以,这是做我想做的最好的方法。

这是我的代码。 有点混乱,因为我只是想工作,所以我可以理解然后再做得更好。 谢谢

#!/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_())

答案是安装编解码器以播放.ogg文件。 感谢@ekhumoro。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM