/
videoplayer.cpp
133 lines (109 loc) · 3.73 KB
/
videoplayer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "videoplayer.h"
#include <QtWidgets>
#include <qvideowidget.h>
#include <qvideosurfaceformat.h>
#include <QDebug>
VideoPlayer::VideoPlayer(QWidget *parent)
: QWidget(parent)
, mediaPlayer(0, QMediaPlayer::VideoSurface)
, playButton(0)
, positionSlider(0)
, errorLabel(0)
{
QVideoWidget *videoWidget = new QVideoWidget;
QAbstractButton *openButton = new QPushButton(tr("Open..."));
connect(openButton, SIGNAL(clicked()), this, SLOT(openFile()));
playButton = new QPushButton;
playButton->setEnabled(false);
playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
connect(playButton, SIGNAL(clicked()),
this, SLOT(play()));
positionSlider = new QSlider(Qt::Horizontal);
positionSlider->setRange(0, 0);
connect(positionSlider, SIGNAL(sliderMoved(int)),
this, SLOT(setPosition(int)));
errorLabel = new QLabel;
errorLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
QBoxLayout *controlLayout = new QHBoxLayout;
controlLayout->setMargin(0);
controlLayout->addWidget(openButton);
controlLayout->addWidget(playButton);
controlLayout->addWidget(positionSlider);
QBoxLayout *layout = new QVBoxLayout;
layout->addWidget(videoWidget);
layout->addLayout(controlLayout);
layout->addWidget(errorLabel);
setLayout(layout);
mediaPlayer.setVideoOutput(videoWidget);
connect(&mediaPlayer, SIGNAL(stateChanged(QMediaPlayer::State)),
this, SLOT(mediaStateChanged(QMediaPlayer::State)));
connect(&mediaPlayer, SIGNAL(positionChanged(qint64)), this, SLOT(positionChanged(qint64)));
connect(&mediaPlayer, SIGNAL(durationChanged(qint64)), this, SLOT(durationChanged(qint64)));
connect(&mediaPlayer, SIGNAL(error(QMediaPlayer::Error)), this, SLOT(handleError()));
QFileInfo info("C:\\Users\\MichaU\\Desktop\\ResonanceP1.mp4");
QMediaPlaylist *lista = new QMediaPlaylist;
lista->addMedia(QUrl::fromLocalFile(info.absoluteFilePath()));
mediaPlayer.setPlaylist(lista);
mediaPlayer.play();
connect(lista,SIGNAL(currentIndexChanged(int)),this,SLOT(ChangePlaylist()));
}
VideoPlayer::~VideoPlayer()
{
}
void VideoPlayer::openFile()
{
errorLabel->setText("File not opened");
QString fileName = QFileDialog::getOpenFileName(this, tr("Open Movie"),QDir::homePath());
if (!fileName.isEmpty()) {
mediaPlayer.setMedia(QUrl::fromLocalFile(fileName));
playButton->setEnabled(true);
}
}
void VideoPlayer::play()
{
switch(mediaPlayer.state()) {
case QMediaPlayer::PlayingState:
mediaPlayer.pause();
break;
default:
mediaPlayer.play();
break;
}
}
void VideoPlayer::mediaStateChanged(QMediaPlayer::State state)
{
switch(state) {
case QMediaPlayer::PlayingState:
playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPause));
break;
default:
playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
break;
}
}
void VideoPlayer::positionChanged(qint64 position)
{
positionSlider->setValue(position);
}
void VideoPlayer::durationChanged(qint64 duration)
{
positionSlider->setRange(0, duration);
}
void VideoPlayer::setPosition(int position)
{
mediaPlayer.setPosition(position);
}
void VideoPlayer::handleError()
{
playButton->setEnabled(false);
errorLabel->setText("Error: " + mediaPlayer.errorString());
}
void VideoPlayer::ChangePlaylist()
{
QFileInfo info1("C:\\Users\\MichaU\\Desktop\\ResonanceP2.mp4");
QMediaPlaylist *lista1 = new QMediaPlaylist;
lista1->addMedia(QUrl::fromLocalFile(info1.absoluteFilePath()));
lista1->setPlaybackMode(QMediaPlaylist::CurrentItemInLoop);
mediaPlayer.setPlaylist(lista1);
mediaPlayer.play();
}