一、前言

对于Windows平台,Qt有特有的模块来调用ActivityX控件

比如调用WMP,首先需要知道WMP的CLSID:​​{6BF52A52-394A-11D3-B153-00C04F79FAA6}​


二、效果展示

Qt操控Windows Media Player_desktop


三、具体步骤

1、在工程文件(.pro)中添加库

QT +=

Qt操控Windows Media Player_d3_02

2、添加头文件

#include <QAxObject>  //用于操作WMP对象
#include <QAxWidget> //用于显示WMP

3、添加WMP

axWidget = new QAxWidget;
axWidget->setControl("{6BF52A52-394A-11d3-B153-00C04F79FAA6}");

4、控制WMP

//使用dynamicCall()来调用成员函数,setProporty()来调用属性参数

axWidget->dynamicCall("URL",path);//设置组件的属性,也可利用setProperty()函数


QAxObject *controls = axWidget->querySubObject("controls");
controls->dynamicCall("stop()"); //调用成员函数

Windows Media Player ActiveX 常用的属性和方法

属性/方法名

说明

[基本属性]

URL:String;

指定媒体位置,本机或网络地址

uiMode:String;

播放器界面模式,可为Full, Mini, None, Invisible

playState:integer;

播放状态,1=停止,2=暂停,3=播放,6=正在缓冲,9=正在连接,10=准备就绪

enableContextMenu:Boolean;

启用/禁用右键菜单

fullScreen:boolean;

是否全屏显示

[controls]

wmp.controls //播放器基本控制

controls.play;

播放

controls.pause;

暂停

controls.stop;

停止

controls.currentPosition:double;

当前进度

controls.currentPositionString:string;

当前进度,字符串格式。如“00:23”

controls.fastForward;

快进

controls.fastReverse;

快退

controls.next;

下一曲

controls.previous;

上一曲

[settings]

wmp.settings

settings.volume:integer;

音量,0-100

settings.autoStart:Boolean;

是否自动播放

settings.mute:Boolean;

是否静音

settings.playCount:integer;

播放次数

[currentMedia]

wmp.currentMedia //当前媒体属性

currentMedia.duration:double;

媒体总长度

currentMedia.durationString:string;

媒体总长度,字符串格式。如“03:24”

currentMedia.getItemInfo(const string);

获取当前媒体信息"Title"=媒体标题,“Author”=艺术家,“Copyright”=版权信息,“Description”=媒体内容描述,“Duration”=持续时间(秒),“FileSize”=文件大小,“FileType”=文件类型,“sourceURL”=原始地址

currentMedia.setItemInfo(const string);

通过属性名设置媒体信息

currentMedia.name:string;

同 currentMedia.getItemInfo(“Title”)

[currentPlaylist]

wmp.currentPlaylist //当前播放列表属性

currentPlaylist.count:integer;

当前播放列表所包含媒体数

currentPlaylist.Item[integer];

获取或设置指定项目媒体信息,其子属性同wmp.currentMedia

我们并不知道wmp有那些参数和成员函数,对此,Qt提供了​​dumpdoc​​命令来自动生成对应CLSID控件的说明文档:

打开编译器命令行,输入指令:dumpdoc {xxxx} -o xxx.html​​

Qt操控Windows Media Player_d3_03


Qt操控Windows Media Player_d3_04


Qt操控Windows Media Player_desktop_05


四、详细代码

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QDebug>
#include <QFileDialog>
#include <QDesktopWidget>
#include <QAxObject>
#include <QAxWidget>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();

void InitUI();
QAxWidget* axWidget;

protected:
void closeEvent(QCloseEvent *event);


private slots:
void on_btn_openFile_clicked();

void on_btn_play_clicked();

void on_btn_pause_clicked();

void on_btn_stop_clicked();

void on_SoundVolumn_valueChanged(int value);

private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);

InitUI();
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::InitUI()
{
this->setWindowFlags(Qt::WindowStaysOnTopHint); //窗口始终置顶

ui->SoundVolumn->setRange(0,100);
ui->SoundVolumn->setValue(0);

axWidget = new QAxWidget;
axWidget->setControl("{6BF52A52-394A-11d3-B153-00C04F79FAA6}");

QDesktopWidget* desktop = QApplication::desktop();
axWidget->setGeometry(desktop->screenGeometry());

}

void MainWindow::closeEvent(QCloseEvent *event)
{
axWidget->close();
}

void MainWindow::on_btn_openFile_clicked()
{
QString path = QFileDialog::getOpenFileName(this,"选择要播放的文件");
axWidget->dynamicCall("URL",path);//设置组件的属性,也可利用setProperty()函数

//停止播放
QAxObject *controls = axWidget->querySubObject("controls");
controls->dynamicCall("stop()");

//音量为0
QAxObject *settings = axWidget->querySubObject("settings");
settings->dynamicCall("volume",0);

//视频信息
QAxObject *currentMedia = axWidget->querySubObject("currentMedia");
QString Title = currentMedia->dynamicCall("getItemInfo(QString)","title").toString();
QString Author = currentMedia->dynamicCall("getItemInfo(QString)","author").toString();
QString Copyright = currentMedia->dynamicCall("getItemInfo(QString)","copyright").toString();
QString Description = currentMedia->dynamicCall("getItemInfo(QString)","description").toString();
QString Duration = currentMedia->dynamicCall("getItemInfo(QString)","duration").toString();
QString FileSize = currentMedia->dynamicCall("getItemInfo(QString)","fileSize").toString();
QString FileType = currentMedia->dynamicCall("getItemInfo(QString)","fileType").toString();
QString sourceURL = currentMedia->dynamicCall("getItemInfo(QString)","sourceURL").toString();
ui->InfoShow->clear();
ui->InfoShow->append("媒体标题: " + Title);
ui->InfoShow->append("艺术家: " + Author);
ui->InfoShow->append("版权信息: " + Copyright);
ui->InfoShow->append("媒体内容描述: " + Description);
ui->InfoShow->append("持续时间(秒): " + Duration);
ui->InfoShow->append("文件大小: " + FileSize);
ui->InfoShow->append("文件类型: " + FileType);
ui->InfoShow->append("原始地址: " + sourceURL);

axWidget->show();
}

void MainWindow::on_btn_play_clicked()
{
//获取组件的子对象
QAxObject *controls = axWidget->querySubObject("controls");
//调用组件的方法
controls->dynamicCall("play()");
}

void MainWindow::on_btn_pause_clicked()
{
//获取组件的子对象
QAxObject *controls = axWidget->querySubObject("controls");
//调用组件的方法
controls->dynamicCall("pause()");
}

void MainWindow::on_btn_stop_clicked()
{
//获取组件的子对象
QAxObject *controls = axWidget->querySubObject("controls");
//调用组件的方法
controls->dynamicCall("stop()");
}

void MainWindow::on_SoundVolumn_valueChanged(int value)
{
QAxObject *settings = axWidget->querySubObject("settings");
settings->dynamicCall("volume",value);
}