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 134 135 136 137
|
// Copyright (C) 2017 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include "videoplayer.h"
#include <QBoxLayout>
#include <QFileDialog>
#include <QHBoxLayout>
#include <QLabel>
#include <QMediaPlayer>
#include <QPushButton>
#include <QSizePolicy>
#include <QSlider>
#include <QStandardPaths>
#include <QString>
#include <QStyle>
#include <QVideoWidget>
VideoPlayer::VideoPlayer(QWidget *parent) : QWidget(parent)
{
m_mediaPlayer = new QMediaPlayer(this);
QVideoWidget *videoWidget = new QVideoWidget;
QAbstractButton *openButton = new QPushButton(tr("Open..."));
connect(openButton, &QAbstractButton::clicked, this, &VideoPlayer::openFile);
m_playButton = new QPushButton;
m_playButton->setEnabled(false);
m_playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
connect(m_playButton, &QAbstractButton::clicked, this, &VideoPlayer::play);
m_positionSlider = new QSlider(Qt::Horizontal);
m_positionSlider->setRange(0, 0);
connect(m_positionSlider, &QAbstractSlider::sliderMoved, this, &VideoPlayer::setPosition);
m_errorLabel = new QLabel;
m_errorLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
QBoxLayout *controlLayout = new QHBoxLayout;
controlLayout->setContentsMargins(0, 0, 0, 0);
controlLayout->addWidget(openButton);
controlLayout->addWidget(m_playButton);
controlLayout->addWidget(m_positionSlider);
QBoxLayout *layout = new QVBoxLayout;
layout->addWidget(videoWidget);
layout->addLayout(controlLayout);
layout->addWidget(m_errorLabel);
setLayout(layout);
m_mediaPlayer->setVideoOutput(videoWidget);
connect(m_mediaPlayer, &QMediaPlayer::playbackStateChanged, this,
&VideoPlayer::mediaStateChanged);
connect(m_mediaPlayer, &QMediaPlayer::positionChanged, this, &VideoPlayer::positionChanged);
connect(m_mediaPlayer, &QMediaPlayer::durationChanged, this, &VideoPlayer::durationChanged);
connect(m_mediaPlayer, &QMediaPlayer::errorChanged, this, &VideoPlayer::handleError);
}
VideoPlayer::~VideoPlayer() { }
void VideoPlayer::openFile()
{
QFileDialog fileDialog(this);
fileDialog.setAcceptMode(QFileDialog::AcceptOpen);
fileDialog.setWindowTitle(tr("Open Movie"));
fileDialog.setDirectory(QStandardPaths::standardLocations(QStandardPaths::MoviesLocation)
.value(0, QDir::homePath()));
if (fileDialog.exec() == QDialog::Accepted)
setUrl(fileDialog.selectedUrls().constFirst());
}
void VideoPlayer::setUrl(const QUrl &url)
{
m_errorLabel->setText(QString());
setWindowFilePath(url.isLocalFile() ? url.toLocalFile() : QString());
m_mediaPlayer->setSource(url);
m_playButton->setEnabled(true);
}
void VideoPlayer::play()
{
switch (m_mediaPlayer->playbackState()) {
case QMediaPlayer::PlayingState:
m_mediaPlayer->pause();
break;
default:
m_mediaPlayer->play();
break;
}
}
void VideoPlayer::mediaStateChanged(QMediaPlayer::PlaybackState state)
{
switch (state) {
case QMediaPlayer::PlayingState:
m_playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPause));
break;
default:
m_playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
break;
}
}
void VideoPlayer::positionChanged(qint64 position)
{
m_positionSlider->setValue(position);
}
void VideoPlayer::durationChanged(qint64 duration)
{
m_positionSlider->setRange(0, duration);
}
void VideoPlayer::setPosition(int position)
{
m_mediaPlayer->setPosition(position);
}
void VideoPlayer::handleError()
{
if (m_mediaPlayer->error() == QMediaPlayer::NoError)
return;
m_playButton->setEnabled(false);
const QString errorString = m_mediaPlayer->errorString();
QString message = "Error: ";
if (errorString.isEmpty())
message += " #" + QString::number(int(m_mediaPlayer->error()));
else
message += errorString;
m_errorLabel->setText(message);
}
#include "moc_videoplayer.cpp"
|