QMediaPlayer无法在dll中工作

时间:2018-08-01 15:04:16

标签: c++ qt dll qmediaplayer

QMediaPlayer在exe中静态编译-正常工作。 在dll中编译-不起作用。

Qt 5.7.1,静态版本。


tst_play_exe.pro

QT += widgets multimedia
CONFIG += c++14
TARGET = tst_play_exe
SOURCES = tst_play_exe.cpp

tst_play_exe.cpp

#include <QApplication>
#include <QPushButton>
#include <QMediaPlayer>

QMediaPlayer ply;

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);
    QPushButton btn("play"); btn.resize(100, 30);

    QObject::connect(&btn, &QPushButton::clicked, &ply, &QMediaPlayer::play);
    ply.setMedia(QUrl::fromLocalFile("d:/temp/19/example/example.mp3"));

    btn.show();
    return app.exec();
}// main

有效


tst_play_dll.pro

QT += core multimedia
CONFIG += c++14

TARGET = tst_play_dll
TEMPLATE = lib
CONFIG += dll

HEADERS += \
    tst_play_dll.h   \

SOURCES += \
    tst_play_dll.cpp \

tst_play_dll.h

#ifndef TST_PLAY_DLL
#define TST_PLAY_DLL

#endif // TST_PLAY_DLL

tst_play_dll.cpp

#include <QMediaPlayer>
#include "tst_play_dll.h"

#define VERSION 0
#define ERR -1000
#define EXTERN_DLL_EXPORT extern "C" __declspec(dllexport)

typedef long long si64;
typedef const char* cstr;

QMediaPlayer *ply = nullptr; /* !!! */

EXTERN_DLL_EXPORT int  version(void) { return VERSION; }
EXTERN_DLL_EXPORT void initial(void) { if(!ply) { ply = new QMediaPlayer; }}
EXTERN_DLL_EXPORT void destroy(void) { delete ply; ply = nullptr; }

EXTERN_DLL_EXPORT void open(cstr fnme)
    { initial(); ply->setMedia(QUrl::fromLocalFile(QString(fnme))); }
EXTERN_DLL_EXPORT void play(cstr fnme)
    {  open(fnme); ply->play(); }
EXTERN_DLL_EXPORT void rest(void)    { if(ply) { ply->pause(); }}
EXTERN_DLL_EXPORT void cont(void)    { if(ply) { ply->play();  }}
EXTERN_DLL_EXPORT void stop(void)    { if(ply) { ply->stop();  }}
EXTERN_DLL_EXPORT si64 lgth(void)
    { return ply == nullptr ? ERR : ply->duration(); }

EXTERN_DLL_EXPORT cstr lerr(void)
    { return ply == nullptr ? nullptr : qPrintable(ply->errorString()); }

不起作用(错误:defaultServiceProvider :: requestService():找不到针对“ org.qt-project.qt.mediaplayer”的服务)。

为什么?如何解决?

1 个答案:

答案 0 :(得分:1)

Qt在创建dll时不会导出插件。

tst_play_dll.pro

QTDIR = C:/Qt/5.7.1

QT += core multimedia
CONFIG += c++14

TARGET = tst_play_dll
TEMPLATE = lib
CONFIG += dll

HEADERS += \
    tst_play_dll.h   \

SOURCES += \
    tst_play_dll.cpp \

LIBS += \
    -L$$QTDIR/plugins/mediaservice                  \
        $$QTDIR/plugins/mediaservice/libdsengine.a  \
        $$QTDIR/lib/libQt5Widgets.a                 \
        -ldxva2 -levr -lmf -lmfplat -lmfuuid -ld3d9 \
    -L$$QTDIR/plugins/audio                         \
        -lstrmiids                                  \

tst_play_dll.h

#ifndef TST_PLAY_DLL
#define TST_PLAY_DLL

#include <QtPlugin>

Q_IMPORT_PLUGIN(DSServicePlugin)

#endif // TST_PLAY_DLL

工作