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
(QMediaplayer具体操作细节和疑问参照:http://blog.csdn.net/qq21497936/article/details/78643466)
使用windows Media Player组件播放.swf格式
(对于具体对com的介绍和操作方式以及qt如何使用,将会在后续详细解说,尽请期待。。。)
播放.swf
头文件 mainwindow.h
源码文件 mainwindow.cpp
原博主博客地址:http://blog.csdn.net/qq21497936
本文章博客地址:http://blog.csdn.net/qq21497936/article/details/78651732
本文章博客地址: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
相关文章推荐
- Qt实用技巧:使用QMediaPlayer和Windows自带组件播放swf、rmvb、mpg、mp4等视频文件
- Qt实用技巧:使用QMediaPlayer播放mp4文件
- VS2010MFC对话框程序中使用Windows Media Player播放音频或视频文件
- html制作简单框架网页二 实现自己的影音驿站 操作步骤及源文件下载 (可播放mp4、avi、mpg、asx、swf各种文件的视频播放代码)
- ios播放视频文件一般使用 MPMoviePlayerViewController 和 MPMoviePlayerController
- Window 下 Qt5 使用QMediaplayer 进行视频播放 流播放问题
- QT QMediaPlayer 播放音视频文件
- 【Qt】调用WindowsMediaPlayer进行视频播放=文章未整理=
- Window 下 Qt5 使用QMediaplayer 进行视频播放 流播放问题
- 播放视频音频文件使用 MPMoviePlayerViewController
- 如何让windows media player 自动播放视频文件
- C#使用Windows Media Player播放音频文件
- 使用php文件读取视频stream并用flowplayer播放
- MFC使用Windows media player播放声音文件
- 在系统注册realone的组件,使Windows Media Player播放rm文件
- PPT中插入avi、mpg、flv、swf及rm、rmvb等视频文件的方法
- Windows Media Player播放AVI视频文件时,只有声音,没有图像,问题解决
- 播放视频音频文件使用 MPMoviePlayerViewController
- Android 使用VDPlayerSDK组件实现播放视频(一)
- 【Delphi】从内存(MemoryStream)使用WMP(WindowsMediaPlayer)控件播放视频音频(Play Video with WMP from MemoryStream)