Qt版本为5.2,QMediaPlayer在下面的代码中不起作用(不播放.mp3音频文件):
#include <QCoreApplication>
#include <QMediaPlayer>
#include <QGuiApplication>
int main(int argc, char **argv)
{
// QGuiApplication app(argc, argv);
QCoreApplication app(argc, argv);
QMediaPlayer *player = new QMediaPlayer(&app);
player->setMedia(QUrl::fromLocalFile("some_audio_file.mp3"));
player->play();
return app.exec();
}
相同的代码在Windows下运行,但是当使用QGuiApplication时,QMediaPlayer将播放音频文件。
下面是.pro文件,请注意,'gui'用于'QGuiApplication'。
QT += gui multimedia
CONFIG -= app_bundle
CONFIG += console
TEMPLATE = app
TARGET = test3
INCLUDEPATH += .
# Input
SOURCES += test.cpp