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

Qt实用技巧:使用QMediaPlayer和Windows自带组件播放swf、rmvb、mpg、mp4等视频文件
原创 2017年11月28日 09:56:51
标签:
swf /
qt使用windows组件 /
组件 /
rmvb /
播放器
32
原博主博客地址: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如何使用,将会在后续详细解说,尽请期待。。。)
效果图
播放.mp4
 
播放.swf
 
关键操作
组件初始化操作
[cpp] view plain copy
1. // 初始化QAxWidget控件框架相关  
2. _pAxWidget = new QAxWidget(this);  
3. _pAxWidget->setObjectName(QString::fromUtf8("axWidget"));  
4. _pAxWidget->setProperty("geometry", QVariant(QRect(0,0,1024,768)));  
5. // 绑定控件,下面使用UUID,共4种方式:UUID; Control's class name; Control's full name; from afile  
6. _pAxWidget->setControl(QString::fromUtf8("{d27cdb6e-ae6d-11cf-96b8-444553540000}"));  
组件窗口大小跟随窗口
[cpp] view plain copy
1. void MainWindow::resizeEvent(QResizeEvent *)  
2. {  
3.    // 更新_pAxWidget内部控件的窗口持续大小  
4.    _pAxWidget->setProperty("geometry", QVariant(_pAxWidget->rect()));  
5. }  




源代码
工程文件.pro额外添加
[cpp] view plain copy
1. QT += multimedia  
2. QT += multimediawidgets  
3. QT += axcontainer  
头文件 mainwindow.h
[cpp] view plain copy
1. #ifndef MAINWINDOW_H  
2. #define MAINWINDOW_H  
3.  
4. #include <QMainWindow>  
5. #include <QMediaPlayer>  
6. #include <QVideoWidget>  
7. #include <QMediaPlaylist>  
8. #include <QAxWidget>  
9. #include <QResizeEvent>  
10.  
11. namespace Ui {  
12. class MainWindow;  
13. }  
14.  
15. class MainWindow : public QMainWindow  
16. {  
17.    Q_OBJECT  
18.  
19. public:  
20.    explicit MainWindow(QWidget *parent = 0);  
21.    ~MainWindow();  
22.  
23. protected:  
24.    void resizeEvent(QResizeEvent *);  
25.  
26. private slots:  
27.    void btnClicked();  
28.  
29. private:  
30.    Ui::MainWindow *ui;  
31.    QAxWidget * _pAxWidget;  
32.    QVideoWidget * _pVideoWidget;  
33.    QMediaPlayer * _pMediaPlayer;  
34.    QMediaPlaylist * _pMediaPlaylist;  
35. };  
36.  
37. #endif // MAINWINDOW_H  
源码文件 mainwindow.cpp
[cpp] view plain copy
1. #include "mainwindow.h"  
2. #include "ui_mainwindow.h"  
3.  
4. #include <QFileDialog>  
5. #include <QDebug>  
6. #include <QAxWidget>  
7. #include <QHBoxLayout>  
8. #include <QVBoxLayout>  
9. #include <QPushButton>  
10.  
11. MainWindow::MainWindow(QWidget *parent) :  
12.    QMainWindow(parent),  
13.    ui(new Ui::MainWindow)  
14. {  
15.    ui->setupUi(this);  
16.    // 初始化QAxWidget控件框架相关  
17.    _pAxWidget = new QAxWidget(this);  
18.    _pAxWidget->setObjectName(QString::fromUtf8("axWidget"));  
19.    _pAxWidget->setProperty("geometry", QVariant(QRect(0,0,1024,768)));  
20.    // 绑定控件,下面使用UUID,共4种方式:UUID; Control's class name; Control's full name; from afile  
21.    _pAxWidget->setControl(QString::fromUtf8("{d27cdb6e-ae6d-11cf-96b8-444553540000}"));  
22.    _pAxWidget->hide();  
23.    // 初始化QMediaPlayer框架相关  
24.    _pVideoWidget = new QVideoWidget(this);  
25.    _pMediaPlayer = new QMediaPlayer(this);  
26.    _pMediaPlaylist = new QMediaPlaylist();  
27.    _pMediaPlayer->setVideoOutput(_pVideoWidget);  
28.  
29.    QVBoxLayout * pLayout = new QVBoxLayout();  
30.    QHBoxLayout * pLayout2 = new QHBoxLayout();  
31.    QPushButton * pPushButton = new QPushButton();  
32.    pPushButton->setText("打开播放文件");  
33.    connect(pPushButton, SIGNAL(clicked()), this, SLOT(btnClicked()));  
34.    pLayout2->addStretch(1);  
35.    pLayout2->addWidget(pPushButton);  
36.  
37.    // 添加到总体布局  
38.    pLayout->addWidget(_pAxWidget, 1);  
39.    pLayout->addWidget(_pVideoWidget, 1);  
40.    pLayout->addLayout(pLayout2);  
41.    ui->centralWidget->setLayout(pLayout);  
42. }  
43.  
44. MainWindow::~MainWindow()  
45. {  
46.    delete ui;  
47. }  
48.  
49. void MainWindow::resizeEvent(QResizeEvent *)  
50. {  
51.    // 更新_pAxWidget内部控件的窗口持续大小  
52.    _pAxWidget->setProperty("geometry", QVariant(_pAxWidget->rect()));  
53. }  
54.  
55. void MainWindow::btnClicked()  
56. {  
57.    QString path = QFileDialog::getOpenFileName(this, "打开播放文件", ".", "所有文件(*.*)");  
58.    if(path.isEmpty())  
59.        return;  
60.    // 目前只试过这几种格式,window Media Player(window自带播放器) 可播放的格式,都可以使用 _pAxWidget(com组件)播放  
61.    if (path.right(4)==".swf"  
62.            || path.right(5)==".rmvb"  
63.            || path.right(4)==".mpg"  
64.            || path.right(4)==".mp4")  
65.    {  
66.        if(path.right(4)==".swf")  
67.        {  
68.            _pAxWidget->dynamicCall("LoadMovie(int,const QString&)", 0, path);  
69.            _pAxWidget->dynamicCall("Loop",false);  
70.            _pAxWidget->show();  
71.            _pVideoWidget->hide();  
72.        }else  
73.        {  
74.            _pMediaPlaylist->clear();  
75.            _pMediaPlaylist->addMedia(QUrl::fromLocalFile(path));  
76.            _pMediaPlaylist->setCurrentIndex(0);  
77.            _pMediaPlayer->setPlaylist(_pMediaPlaylist);  
78.            _pMediaPlayer->play();  
79.            _pVideoWidget->show();  
80.            _pAxWidget->hide();  
81.        }  
82.    }  
83. }  

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值