zl程序教程

您现在的位置是:首页 >  其他

当前栏目

Pyhon:PyQt5 QMediaPlayer 错误解决

错误 解决 PyQT5 QMediaPlayer
2023-06-13 09:17:23 时间

调试了一上午,然后各种查询,错误还算是解决了。 错误代码:

    def PlayMp3(self):
        self.player = QMediaPlayer()    
        self.player.setMedia(QMediaContent(QUrl.fromLocalFile("D:\Python\01.mp3")))
        #self.player.setVolume(25)
        self.player.play()  

错误提示:

DirectShowPlayerService::doSetUrlSource: Unresolved error code 0x8007007b (??????????????????????????)

错误一般代表:路径错误或者底层库错误,底层库为:LAV filter,需要安装。 解决方案:

    def PlayMp3(self):
        self.player = QMediaPlayer()    
        self.player.setMedia(QMediaContent(QUrl.fromLocalFile("D:\\Python\\01.mp3")))
        #self.player.setVolume(25)
        self.player.play()      
    def PlayMp3(self):
        self.player = QMediaPlayer()    
        self.player.setMedia(QMediaContent(QUrl.fromLocalFile("D:/Python/01.mp3")))
        #self.player.setVolume(25)
        self.player.play()  

各种搜索问题,很奇怪。 在Qt(C++)官方例子中,代码样例如下:

player = new QMediaPlayer;
connect(player, SIGNAL(positionChanged(qint64)), this, SLOT(positionChanged(qint64)));
player->setMedia(QUrl::fromLocalFile("/Users/me/Music/coolsong.mp3"));
player->setVolume(50);
player->play();

我通过读取文件,打印路径时,打印如下;

D:\Python\ui.py

之前读取本地图片是写过如下代码,测试:

self.m_photo = PhotoImage(file = "D:\Python\\66logo.png")
self.m_label = Label(self,text = "Python学习",font = ('Arial', 15),bg= "blue",fg = "red",width = 240,height = 200,compound = CENTER)

然后再次 测试,代码如下:

    def PlayMp3(self):
        self.player = QMediaPlayer()    
        self.player.setMedia(QMediaContent(QUrl.fromLocalFile("D:\Python\\01.mp3")))
        #self.player.setVolume(25)
        self.player.play()  

问题原因:在 Python中,’\0’,或者’\x’……等等一系列表示特殊字符,需要所以需要转义‘\x’,以后注意。