Qt QMediaPlayer(铺满整个窗口)
2016-08-05 00:42
621 查看
一、QMediaPlayer的定义
(1)QMediaPlayer 官方英文文档链接
http://doc.qt.io/qt-5/qmediaplayer.html
The QMediaPlayer class allows the playing of a media source.
The QMediaPlayer class is a high level media playback class.
It can be used to playback such content as songs, movies and internet radio. The content to playback is specified as a QMediaContent object,
which can be thought of as a main or canonical URL with additional information attached. When provided with a QMediaContent playback
may be able to commence.
QMediaPlayer类是用来做"流媒体播放的"。
QMediaPlayer类是一个高级的多媒体播放类.它可以用来播放歌曲、电影和网络收音机。播放的内容被指定为一个QMediaContent对象,它被看作是一个携带了附加信息的主要或标准的链接。当被提供类一个QMediaContent之后,播放就可以开始了。
(2)QVideoWidget 官方英文文档链接
http://doc.qt.io/qt-5/qvideowidget.html
The QVideoWidget class provides a widget which presents video
produced by a media object.
Attaching a QVideoWidget to a QMediaObject allows
it to display the video or image output of that media object. AQVideoWidget is
attached to media object by passing a pointer to the QMediaObject in
its constructor, and detached by destroying the QVideoWidget.
QVideoWidget类提供了一个显示媒体对象产生的视频的控件。
附到QVideoWidget的QMediaObject对象被允许显示它所输出的视频或图像。QVideoWidget在它的构造函数中通过指针的方式附加到QMediaObject。并且在QVideoWidget销毁的时候分离。
二、代码实战
(1)MediaplayerDemo.pro,需要添加2个库文件:
multimedia 的作用是添加 QMediaPlayer、QMediaPlaylist 的支持
multimediawidgets 的作用是添加 QVideoWidget 的支持
(2)mainwindow.h
(3)mainwindow.cpp
三、说明
视频默认按原大小显示,不会随着QVideoWidget的缩放而改变,要想视频根据QVideoWidget动态变化,需要设置 “AspectRatioMode” 参数。
enum Qt::AspectRatioMode
This enum type defines what happens to the aspect ratio when scaling an rectangle.
这个枚举类型定义了,当拉伸QVideoWidget的时候,图片(或视频)的比例会发生什么变化。
(1)Default
显示区域的大小根据视频的原始大小而决定,不会随着QVideoWidget的缩放而改变。
(2)Qt::IgnoreAspectRatio
填满QVideoWidget
(3)Qt::KeepAspectRatio
视频等比例缩放,并且尽量铺满QVideoWidget,但不超出QVideoWidget的范围。
(4)Qt::KeepAspectRatioByExpanding
视屏等比例缩放,充满整个QVideoWidget,超出QVideoWidget的范围的部分不显示。
(1)QMediaPlayer 官方英文文档链接
http://doc.qt.io/qt-5/qmediaplayer.html
The QMediaPlayer class allows the playing of a media source.
The QMediaPlayer class is a high level media playback class.
It can be used to playback such content as songs, movies and internet radio. The content to playback is specified as a QMediaContent object,
which can be thought of as a main or canonical URL with additional information attached. When provided with a QMediaContent playback
may be able to commence.
QMediaPlayer类是用来做"流媒体播放的"。
QMediaPlayer类是一个高级的多媒体播放类.它可以用来播放歌曲、电影和网络收音机。播放的内容被指定为一个QMediaContent对象,它被看作是一个携带了附加信息的主要或标准的链接。当被提供类一个QMediaContent之后,播放就可以开始了。
(2)QVideoWidget 官方英文文档链接
http://doc.qt.io/qt-5/qvideowidget.html
The QVideoWidget class provides a widget which presents video
produced by a media object.
Attaching a QVideoWidget to a QMediaObject allows
it to display the video or image output of that media object. AQVideoWidget is
attached to media object by passing a pointer to the QMediaObject in
its constructor, and detached by destroying the QVideoWidget.
QVideoWidget类提供了一个显示媒体对象产生的视频的控件。
附到QVideoWidget的QMediaObject对象被允许显示它所输出的视频或图像。QVideoWidget在它的构造函数中通过指针的方式附加到QMediaObject。并且在QVideoWidget销毁的时候分离。
二、代码实战
(1)MediaplayerDemo.pro,需要添加2个库文件:
QT += multimedia multimediawidgets
multimedia 的作用是添加 QMediaPlayer、QMediaPlaylist 的支持
multimediawidgets 的作用是添加 QVideoWidget 的支持
(2)mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QMediaPlayer>
#include <QMediaPlaylist>
#include <QVideoWidget>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
QMediaPlayer *mediaPlayer;
QVideoWidget *videoWidget;
QMediaPlaylist *mediaPlayerlist;
};
#endif // MAINWINDOW_H
(3)mainwindow.cpp
#include "mainwindow.h"
/*
经过测试发现QMediaPlayer
可以播放flv avi
无法播放f4v mp4
*/
//#define VIDEO_PATH "http://main.gslb.ku6.com/s1/ACa7hh1kZcpp1UCTFEnsTw../1468467796693/37e7ec6d945b1459869c0648b6081609/1470326233540/v140/88/12/2dc532a5188e3abdc9eb54332ab0c511-f4v-h264-aac-244-32-129243.0-4574622-1468439556380-93fa78fb0708942892723c641bc2e01d-1-00-00-00.f4v"
#define VIDEO_PATH "C:/Users/Administrator/Desktop/test_baofeng.avi"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
mediaPlayer = new QMediaPlayer();
videoWidget = new QVideoWidget();
this->setCentralWidget(videoWidget);
//给VideoWidget画上黑色的背景,这样会更专业点(默认是灰白色的)
QPalette* palette = new QPalette();
palette->setBrush(QPalette::Background, Qt::black);
videoWidget->setPalette(*palette);
videoWidget->setAutoFillBackground(true);
delete palette;
/*
设置播放视频的比例
enum AspectRatioMode {
IgnoreAspectRatio,
KeepAspectRatio,
KeepAspectRatioByExpanding
};
*/
videoWidget->setAspectRatioMode(Qt::IgnoreAspectRatio);
mediaPlayer->setVideoOutput(videoWidget);
//第一种方法:mediaPlayer直接setMedia()
mediaPlayer->setMedia(QUrl::fromLocalFile(VIDEO_PATH));
mediaPlayer->play();
//第二种方法:通过mediaPlaylist来按列表播放
// mediaPlayerlist = new QMediaPlayerlist(mediaPlayer);
// mediaPlayerlist->addMedia(QUrl::fromLocalFile(VIDEO_PATH));
// mediaPlayerlist->setCurrentIndex(0);
//mediaPlayer->play();
}
MainWindow::~MainWindow(){}
三、说明
视频默认按原大小显示,不会随着QVideoWidget的缩放而改变,要想视频根据QVideoWidget动态变化,需要设置 “AspectRatioMode” 参数。
enum Qt::AspectRatioMode
This enum type defines what happens to the aspect ratio when scaling an rectangle.
这个枚举类型定义了,当拉伸QVideoWidget的时候,图片(或视频)的比例会发生什么变化。
Constant | Value | Description |
Default | -1 | The size is determined by the original size of the video |
Qt::IgnoreAspectRatio | 0 | The size is scaled freely. The aspect ratio is not preserved. |
Qt::KeepAspectRatio | 1 | The size is scaled to a rectangle as large as possible inside a given rectangle, preserving the aspect ratio. |
Qt::KeepAspectRatioByExpanding | 2 | The size is scaled to a rectangle as small as possible outside a given rectangle, preserving the aspect ratio. |
显示区域的大小根据视频的原始大小而决定,不会随着QVideoWidget的缩放而改变。
(2)Qt::IgnoreAspectRatio
填满QVideoWidget
(3)Qt::KeepAspectRatio
视频等比例缩放,并且尽量铺满QVideoWidget,但不超出QVideoWidget的范围。
(4)Qt::KeepAspectRatioByExpanding
视屏等比例缩放,充满整个QVideoWidget,超出QVideoWidget的范围的部分不显示。
相关文章推荐
- 在DELPHI中让WindowsMediaPlayer控件随窗口改变大小。
- QT QMediaPlayer 播放音视频文件
- Window 下 Qt5 使用QMediaplayer 进行视频播放 流播放问题
- qt qmediaplayer m_surface assert
- Qt实用技巧:在Qt Gui程序中嵌入qml界面(可动态覆盖整个窗口)
- 使用 PyQt 转换网页到 PDF(使用QtWebKit加载完毕后,打印整个窗口就行了,真简单!)
- 用MediaPlayer+TextureView封装一个实现全屏、小窗口的视频播放器
- QT —— 如何让QTableWidget表格行列填满整个窗口(mainwindow)
- Qt事件处理机制整个流程--以鼠标在一个窗口中点击为例
- QT中控件如何随着窗口大小的改变而使它充满整个窗口空间
- QT实例之十四(qmediaplayer)
- Qt5.8 QMediaPlayer 音乐播放器
- Qt5.2.0 MinGW480 release静态版编译结果、过程及QtCreator配置分享(realfan的编译方法)(configure -h可以显示帮助)(静态编译mysql插件,需要做一些其它工作,机器上要有mysql)(不编译qtwebkit,就不用安装ActivePerl了)(用到 QMediaPlayer的编译办法)
- Window 下 Qt5 使用QMediaplayer 进行视频播放 流播放问题
- 微信小程序flex布局怎么实现上中下3行铺满整个窗口。
- 用MediaPlayer+TextureView封装一个完美实现全屏、小窗口的视频播放器
- Qt Mediaplayer
- Window 下 Qt5 使用QMediaplayer 进行视频播放 流播放问题
- QT QMediaPlayer报错"org.qt-project.qt.mediaplayer"解决方法
- Qt5 QMediaPlayer 音乐播放器