您的位置:首页 > 编程语言 > Qt开发

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

2017-11-28 09:56 1196 查看
原博主博客地址:http://blog.csdn.net/qq21497936

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

需求

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

原理

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

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

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

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

Demo

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

效果图

播放.mp4



播放.swf



关键操作

组件初始化操作

// 初始化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 <QMainWindow>
#include <QMediaPlayer>
#include <QVideoWidget>
#include <QMediaPlaylist>
#include <QAxWidget>
#include <QResizeEvent>

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 <QFileDialog>
#include <QDebug>
#include <QAxWidget>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPushButton>

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();
}
}
}


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

本文章博客地址:http://blog.csdn.net/qq21497936/article/details/78651732
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐