当前位置:首页 > Windows程序 > 正文

win下使用PyQt的Phonon播放失败

2021-05-25 Windows程序

最近参与了做第三方的Linux网易云音乐客户端时,在win下调试遇到一个致命问题:音乐无法播放,但切到Ubuntu下又是正常的,

使用的代码如下:

#!/usr/bin/env python #-*- coding:utf-8 -*- from PyQt4 import QtGui, QtCore from PyQt4.phonon import Phonon class Window(QtGui.QPushButton):     def __init__(self):         QtGui.QPushButton.__init__(self, ‘Choose File‘)         self.mediaObject = Phonon.MediaObject(self)         self.audioOutput = Phonon.AudioOutput(Phonon.MusicCategory, self)         Phonon.createPath(self.mediaObject, self.audioOutput)         self.mediaObject.stateChanged.connect(self.handleStateChanged)         self.clicked.connect(self.handleButton)     def handleButton(self):         if self.mediaObject.state() == Phonon.PlayingState:             self.mediaObject.stop()         else:             path = QtGui.QFileDialog.getOpenFileName(self, self.text())             #path = "test.mp3"             if path:                 print path                 self.mediaObject.setCurrentSource(Phonon.MediaSource(path))                 print self.mediaObject.currentSource().fileName()                 self.mediaObject.play()     def handleStateChanged(self, newstate, oldstate):         if newstate == Phonon.PlayingState:             self.setText(‘Stop‘)         elif newstate == Phonon.StoppedState:             self.setText(‘Choose File‘)         elif newstate == Phonon.ErrorState:             source = self.mediaObject.currentSource().fileName()             print ‘ERROR: could not play:‘, source.toLocal8Bit().data()             print self.mediaObject.errorString().toLocal8Bit().data()#查看报错的具体信息 if __name__ == ‘__main__‘:     import sys     app = QtGui.QApplication(sys.argv)     app.setApplicationName(‘Phonon‘)     win = Window()     win.resize(200, 100)     win.show()     sys.exit(app.exec_())

就是这段代码在ubuntu下正常,在win下报错

通过查看   media的errorString

print self.mediaObject.errorString().toLocal8Bit().data()#查看报错的具体信息

得到了错误信息是:

Pins cannot connect due to not supporting the same transport

经查找各方资料,得出的结论(已验证)是:音乐文件的ID3 tag有问题,(具体的mp3文件的结构见:MP3文件格式)

在stackoverflow上看到了c++版的去掉ID3 tag的办法:

void removeTags(UDJ::DataStore::song_info_t& song){   static int fileCount =0;   if(song.source.fileName().endsWith(".mp3")){     UDJ::Logger::instance()->log("On windows and got mp3, copying and striping metadata tags");     QString tempCopy = QDesktopServices::storageLocation(QDesktopServices::TempLocation) + "/striped" + QString::number(fileCount) +".mp3";     if(QFile::exists(tempCopy)){       UDJ::Logger::instance()->log("Prevoius file existed, deleting now");       if(QFile::remove(tempCopy)){         UDJ::Logger::instance()->log("File removal worked");       }     }     bool fileCopyWorked = QFile::copy(song.source.fileName(), tempCopy);     if(!fileCopyWorked){       UDJ::Logger::instance()->log("File copy didn‘t work");       return;     }     TagLib::MPEG::File file(tempCopy.toStdString().c_str());      file.strip();     file.save();     Phonon::MediaSource newSource(tempCopy);     song.source = newSource;     if(fileCount == 3){       fileCount =0;     }     else{       fileCount++;     }   }}

在python中有专门的模块用来处理音频的metadata : mutagen,文档在:mutagen-doc

这里利用Tutorial中给出的示例,去掉了本地音乐文件的ID3 tag:

from mutagen.id3 import ID3 audio = ID3("example.mp3") audio.delete() audio.save()

之后再次调用上面的测试代码,成功播放.

win下使用PyQt的Phonon播放失败

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/70887.html