一、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个库文件:
QT += multimedia multimediawidgets
multimedia 的作用是添加 QMediaPlayer、QMediaPlaylist 的支持
multimediawidgets 的作用是添加 QVideoWidget 的支持
(2)mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
#include
#include
#include
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. |
(1)Default
显示区域的大小根据视频的原始大小而决定,不会随着QVideoWidget的缩放而改变。
(2)Qt::IgnoreAspectRatio
填满QVideoWidget
(3)Qt::KeepAspectRatio
视频等比例缩放,并且尽量铺满QVideoWidget,但不超出QVideoWidget的范围。
(4)Qt::KeepAspectRatioByExpanding
视屏等比例缩放,充满整个QVideoWidget,超出QVideoWidget的范围的部分不显示。