1*4882a593Smuzhiyun /****************************************************************************
2*4882a593Smuzhiyun **
3*4882a593Smuzhiyun ** Copyright (C) 2017 The Qt Company Ltd.
4*4882a593Smuzhiyun ** Contact: https://www.qt.io/licensing/
5*4882a593Smuzhiyun **
6*4882a593Smuzhiyun ** This file is part of the examples of the Qt Toolkit.
7*4882a593Smuzhiyun **
8*4882a593Smuzhiyun ** $QT_BEGIN_LICENSE:BSD$
9*4882a593Smuzhiyun ** Commercial License Usage
10*4882a593Smuzhiyun ** Licensees holding valid commercial Qt licenses may use this file in
11*4882a593Smuzhiyun ** accordance with the commercial license agreement provided with the
12*4882a593Smuzhiyun ** Software or, alternatively, in accordance with the terms contained in
13*4882a593Smuzhiyun ** a written agreement between you and The Qt Company. For licensing terms
14*4882a593Smuzhiyun ** and conditions see https://www.qt.io/terms-conditions. For further
15*4882a593Smuzhiyun ** information use the contact form at https://www.qt.io/contact-us.
16*4882a593Smuzhiyun **
17*4882a593Smuzhiyun ** BSD License Usage
18*4882a593Smuzhiyun ** Alternatively, you may use this file under the terms of the BSD license
19*4882a593Smuzhiyun ** as follows:
20*4882a593Smuzhiyun **
21*4882a593Smuzhiyun ** "Redistribution and use in source and binary forms, with or without
22*4882a593Smuzhiyun ** modification, are permitted provided that the following conditions are
23*4882a593Smuzhiyun ** met:
24*4882a593Smuzhiyun ** * Redistributions of source code must retain the above copyright
25*4882a593Smuzhiyun ** notice, this list of conditions and the following disclaimer.
26*4882a593Smuzhiyun ** * Redistributions in binary form must reproduce the above copyright
27*4882a593Smuzhiyun ** notice, this list of conditions and the following disclaimer in
28*4882a593Smuzhiyun ** the documentation and/or other materials provided with the
29*4882a593Smuzhiyun ** distribution.
30*4882a593Smuzhiyun ** * Neither the name of The Qt Company Ltd nor the names of its
31*4882a593Smuzhiyun ** contributors may be used to endorse or promote products derived
32*4882a593Smuzhiyun ** from this software without specific prior written permission.
33*4882a593Smuzhiyun **
34*4882a593Smuzhiyun **
35*4882a593Smuzhiyun ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36*4882a593Smuzhiyun ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37*4882a593Smuzhiyun ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38*4882a593Smuzhiyun ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39*4882a593Smuzhiyun ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40*4882a593Smuzhiyun ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41*4882a593Smuzhiyun ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42*4882a593Smuzhiyun ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43*4882a593Smuzhiyun ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44*4882a593Smuzhiyun ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45*4882a593Smuzhiyun ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46*4882a593Smuzhiyun **
47*4882a593Smuzhiyun ** $QT_END_LICENSE$
48*4882a593Smuzhiyun **
49*4882a593Smuzhiyun ****************************************************************************/
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun #include "multivideoplayer.h"
52*4882a593Smuzhiyun #include <QtWidgets>
53*4882a593Smuzhiyun #include <QVideoSurfaceFormat>
54*4882a593Smuzhiyun
MultiVideoPlayer(QStringList list)55*4882a593Smuzhiyun MultiVideoPlayer::MultiVideoPlayer(QStringList list)
56*4882a593Smuzhiyun {
57*4882a593Smuzhiyun const QRect screenGeometry = QApplication::desktop()->screenGeometry(this);
58*4882a593Smuzhiyun int i, num = 0;
59*4882a593Smuzhiyun resize(screenGeometry.width(), screenGeometry.height());
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun playerList = new QList<QMediaPlayer*>();
62*4882a593Smuzhiyun videoList = new QList<QVideoWidget*>();
63*4882a593Smuzhiyun hLayoutList = new QList<QHBoxLayout*>();
64*4882a593Smuzhiyun multiPlayList = new QList<QMediaPlaylist*>();
65*4882a593Smuzhiyun urlList = list;
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun exitButton = new QPushButton(tr("Exit"));
68*4882a593Smuzhiyun connect(exitButton, &QAbstractButton::clicked, this, &MultiVideoPlayer::exit);
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun if(urlList.count() > 0){
71*4882a593Smuzhiyun num = qSqrt(urlList.count());
72*4882a593Smuzhiyun if(qreal(num) < qSqrt(list.count())){
73*4882a593Smuzhiyun num++;
74*4882a593Smuzhiyun }
75*4882a593Smuzhiyun }
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun for(i=0; i<num; i++){
78*4882a593Smuzhiyun QHBoxLayout *layout = new QHBoxLayout;
79*4882a593Smuzhiyun hLayoutList->append(layout);
80*4882a593Smuzhiyun }
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun for(i=0; i<urlList.count(); i++){
83*4882a593Smuzhiyun QMediaPlayer *player = new QMediaPlayer(this, QMediaPlayer::VideoSurface);
84*4882a593Smuzhiyun playerList->append(player);
85*4882a593Smuzhiyun QVideoWidget *video = new QVideoWidget();
86*4882a593Smuzhiyun videoList->append(video);
87*4882a593Smuzhiyun QMediaPlaylist *playlist = new QMediaPlaylist(this);
88*4882a593Smuzhiyun multiPlayList->append(playlist);
89*4882a593Smuzhiyun QString str = list.at(i);
90*4882a593Smuzhiyun QFile f(urlList.at(i));
91*4882a593Smuzhiyun QUrl u(str);
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun if(f.exists()){
94*4882a593Smuzhiyun playlist->addMedia(QUrl::fromLocalFile(str));
95*4882a593Smuzhiyun }else if(u.isValid()){
96*4882a593Smuzhiyun playlist->addMedia(u);
97*4882a593Smuzhiyun }
98*4882a593Smuzhiyun playlist->setCurrentIndex(1);
99*4882a593Smuzhiyun playlist->setPlaybackMode(QMediaPlaylist::Loop);
100*4882a593Smuzhiyun player->setPlaylist(playlist);
101*4882a593Smuzhiyun QHBoxLayout *layout = hLayoutList->value(i/num);
102*4882a593Smuzhiyun if(layout){
103*4882a593Smuzhiyun layout->addWidget(video);
104*4882a593Smuzhiyun layout->setMargin(0);
105*4882a593Smuzhiyun layout->setSpacing(0);
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun }
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun QBoxLayout *mainLayout = new QVBoxLayout();
110*4882a593Smuzhiyun mainLayout->setMargin(0);
111*4882a593Smuzhiyun mainLayout->setSpacing(0);
112*4882a593Smuzhiyun for(i=0; i<hLayoutList->count(); i++){
113*4882a593Smuzhiyun QWidget *widget = new QWidget();
114*4882a593Smuzhiyun widget->setLayout(hLayoutList->value(i));
115*4882a593Smuzhiyun mainLayout->addWidget(widget);
116*4882a593Smuzhiyun }
117*4882a593Smuzhiyun mainLayout->addWidget(exitButton);
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun QWidget *widget = new QWidget();
120*4882a593Smuzhiyun widget->setLayout(mainLayout);
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun setCentralWidget(widget);
123*4882a593Smuzhiyun setWindowState(Qt::WindowMaximized);
124*4882a593Smuzhiyun setWindowFlags(Qt::FramelessWindowHint);
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun QPalette palette = QWidget::palette();
127*4882a593Smuzhiyun palette.setColor(QPalette::Window, Qt::black);
128*4882a593Smuzhiyun setPalette(palette);
129*4882a593Smuzhiyun }
130*4882a593Smuzhiyun
~MultiVideoPlayer()131*4882a593Smuzhiyun MultiVideoPlayer::~MultiVideoPlayer()
132*4882a593Smuzhiyun {
133*4882a593Smuzhiyun }
134*4882a593Smuzhiyun
play()135*4882a593Smuzhiyun void MultiVideoPlayer::play()
136*4882a593Smuzhiyun {
137*4882a593Smuzhiyun for(int i=0; i<urlList.count(); i++){
138*4882a593Smuzhiyun QVideoWidget *video = videoList->value(i);
139*4882a593Smuzhiyun QMediaPlayer *player = playerList->value(i);
140*4882a593Smuzhiyun if(video && player){
141*4882a593Smuzhiyun player->setVideoOutput(video);
142*4882a593Smuzhiyun video->show();
143*4882a593Smuzhiyun player->play();
144*4882a593Smuzhiyun }
145*4882a593Smuzhiyun }
146*4882a593Smuzhiyun }
147*4882a593Smuzhiyun
mouseDoubleClickEvent(QMouseEvent * e)148*4882a593Smuzhiyun void MultiVideoPlayer::mouseDoubleClickEvent(QMouseEvent *e)
149*4882a593Smuzhiyun {
150*4882a593Smuzhiyun if(e->button() == Qt::LeftButton){
151*4882a593Smuzhiyun if(isFullScreen()){
152*4882a593Smuzhiyun showNormal();
153*4882a593Smuzhiyun }else {
154*4882a593Smuzhiyun showFullScreen();
155*4882a593Smuzhiyun }
156*4882a593Smuzhiyun }
157*4882a593Smuzhiyun }
158*4882a593Smuzhiyun
mouseReleaseEvent(QMouseEvent * e)159*4882a593Smuzhiyun void MultiVideoPlayer::mouseReleaseEvent(QMouseEvent *e)
160*4882a593Smuzhiyun {
161*4882a593Smuzhiyun if(e->button() == Qt::LeftButton){
162*4882a593Smuzhiyun }
163*4882a593Smuzhiyun }
164*4882a593Smuzhiyun
keyReleaseEvent(QKeyEvent * e)165*4882a593Smuzhiyun void MultiVideoPlayer::keyReleaseEvent(QKeyEvent *e)
166*4882a593Smuzhiyun {
167*4882a593Smuzhiyun switch(e->key())
168*4882a593Smuzhiyun {
169*4882a593Smuzhiyun case Qt::Key_VolumeUp:
170*4882a593Smuzhiyun case Qt::Key_Up:
171*4882a593Smuzhiyun break;
172*4882a593Smuzhiyun case Qt::Key_VolumeDown:
173*4882a593Smuzhiyun case Qt::Key_Down:
174*4882a593Smuzhiyun break;
175*4882a593Smuzhiyun }
176*4882a593Smuzhiyun }
177*4882a593Smuzhiyun
exit()178*4882a593Smuzhiyun void MultiVideoPlayer::exit()
179*4882a593Smuzhiyun {
180*4882a593Smuzhiyun qApp->exit(0);
181*4882a593Smuzhiyun }
182*4882a593Smuzhiyun
183