linux qt系统播放swf,Qt实用技巧:使用QMediaPlayer和Windows自带组件播放swf、rmvb、mpg、mp4等视频文件...

若该文为原创文章,未经允许不得转载

原博主博客地址:https://blog.csdn.net/qq21497936

原博主博客导航:https://blog.csdn.net/qq21497936/article/details/102478062

本文章博客地址:http://blog.csdn.net/qq21497936/article/details/78651732

各位读者,知识无穷而人力有穷,要么改需求,要么找专业人士,要么自己研究

目录

需求

原理

相关博客

Demo

效果图

关键操作

源代码

Qt开发专栏:实用技巧(点击传送门)

Qt实用技巧:使用QMediaPlayer和Windows自带组件播放swf、rmvb、mpg、mp4等视频文件

需求

做软件时,点击进入界面需要播放一段视频,该视频的格式可兼容swf、rmvb、mpg、mp4等视频文件。

原理

使用QMediaPlayer播放rmvb、mpg、mp4格式

(QMediaplayer具体操作细节和疑问参照:http://blog.csdn.net/qq21497936/article/details/78643466)

使用windows Media Player组件播放.swf格式

(对于具体对com的介绍和操作方式以及qt如何使用,将会在后续详细解说,尽请期待。。。)

相关博客

《Qt实用技巧:视频播放器控件(不依赖系统编解码)》

《Qt实用技巧:使用OpenCV库操作摄像头拍照、调节参数和视频录制》

《Qt实用技巧:使用OpenCV库的视频播放器(支持播放器操作,如暂停、恢复、停止、时间、进度条拽托等)》

《Qt实用技巧:使用QMediaPlayer播放mp4文件》

《Qt实用技巧:使用QMediaPlayer和Windows自带组件播放swf、rmvb、mpg、mp4等视频文件》

Demo

Demo源码下载途径:http://download.csdn.net/download/qq21497936/10135478

效果图

播放.mp4

a927d2f8cadc264a8a795b6fd4a38b77.png

播放.swf

665137eca4b11949ef165c79c8a77754.png

关键操作

组件初始化操作

// 初始化QAxWidget控件框架相关

_pAxWidget = new QAxWidget(this);

_pAxWidget->setObjectName(QString::fromUtf8("axWidget"));

_pAxWidget->setProperty("geometry", QVariant(QRect(0,0,1024,768)));

// 绑定控件,下面使用UUID,共4种方式:UUID; Control's class name; Control's full name; from afile

_pAxWidget->setControl(QString::fromUtf8("{d27cdb6e-ae6d-11cf-96b8-444553540000}"));

组件窗口大小跟随窗口

void MainWindow::resizeEvent(QResizeEvent *)

{

// 更新_pAxWidget内部控件的窗口持续大小

_pAxWidget->setProperty("geometry", QVariant(_pAxWidget->rect()));

}

源代码

工程文件.pro额外添加

QT += multimedia

QT += multimediawidgets

QT += axcontainer

头文件 mainwindow.h

#ifndef MAINWINDOW_H

#define MAINWINDOW_H

#include #include #include #include #include #include namespace Ui {

class MainWindow;

}

class MainWindow : public QMainWindow

{

Q_OBJECT

public:

explicit MainWindow(QWidget *parent = 0);

~MainWindow();

protected:

void resizeEvent(QResizeEvent *);

private slots:

void btnClicked();

private:

Ui::MainWindow *ui;

QAxWidget * _pAxWidget;

QVideoWidget * _pVideoWidget;

QMediaPlayer * _pMediaPlayer;

QMediaPlaylist * _pMediaPlaylist;

};

#endif // MAINWINDOW_H

源码文件 mainwindow.cpp

#include "mainwindow.h"

#include "ui_mainwindow.h"

#include #include #include #include #include #include MainWindow::MainWindow(QWidget *parent) :

QMainWindow(parent),

ui(new Ui::MainWindow)

{

ui->setupUi(this);

// 初始化QAxWidget控件框架相关

_pAxWidget = new QAxWidget(this);

_pAxWidget->setObjectName(QString::fromUtf8("axWidget"));

_pAxWidget->setProperty("geometry", QVariant(QRect(0,0,1024,768)));

// 绑定控件,下面使用UUID,共4种方式:UUID; Control's class name; Control's full name; from afile

_pAxWidget->setControl(QString::fromUtf8("{d27cdb6e-ae6d-11cf-96b8-444553540000}"));

_pAxWidget->hide();

// 初始化QMediaPlayer框架相关

_pVideoWidget = new QVideoWidget(this);

_pMediaPlayer = new QMediaPlayer(this);

_pMediaPlaylist = new QMediaPlaylist();

_pMediaPlayer->setVideoOutput(_pVideoWidget);

QVBoxLayout * pLayout = new QVBoxLayout();

QHBoxLayout * pLayout2 = new QHBoxLayout();

QPushButton * pPushButton = new QPushButton();

pPushButton->setText("打开播放文件");

connect(pPushButton, SIGNAL(clicked()), this, SLOT(btnClicked()));

pLayout2->addStretch(1);

pLayout2->addWidget(pPushButton);

// 添加到总体布局

pLayout->addWidget(_pAxWidget, 1);

pLayout->addWidget(_pVideoWidget, 1);

pLayout->addLayout(pLayout2);

ui->centralWidget->setLayout(pLayout);

}

MainWindow::~MainWindow()

{

delete ui;

}

void MainWindow::resizeEvent(QResizeEvent *)

{

// 更新_pAxWidget内部控件的窗口持续大小

_pAxWidget->setProperty("geometry", QVariant(_pAxWidget->rect()));

}

void MainWindow::btnClicked()

{

QString path = QFileDialog::getOpenFileName(this, "打开播放文件", ".", "所有文件(*.*)");

if(path.isEmpty())

return;

// 目前只试过这几种格式,window Media Player(window自带播放器) 可播放的格式,都可以使用 _pAxWidget(com组件)播放

if (path.right(4)==".swf"

|| path.right(5)==".rmvb"

|| path.right(4)==".mpg"

|| path.right(4)==".mp4")

{

if(path.right(4)==".swf")

{

_pAxWidget->dynamicCall("LoadMovie(int,const QString&)", 0, path);

_pAxWidget->dynamicCall("Loop",false);

_pAxWidget->show();

_pVideoWidget->hide();

}else

{

_pMediaPlaylist->clear();

_pMediaPlaylist->addMedia(QUrl::fromLocalFile(path));

_pMediaPlaylist->setCurrentIndex(0);

_pMediaPlayer->setPlaylist(_pMediaPlaylist);

_pMediaPlayer->play();

_pVideoWidget->show();

_pAxWidget->hide();

}

}

}

原博主博客地址:https://blog.csdn.net/qq21497936

原博主博客导航:https://blog.csdn.net/qq21497936/article/details/102478062

本文章博客地址:http://blog.csdn.net/qq21497936/article/details/78651732