| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2017 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the examples of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:BSD$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** BSD License Usage |
| 18 | ** Alternatively, you may use this file under the terms of the BSD license |
| 19 | ** as follows: |
| 20 | ** |
| 21 | ** "Redistribution and use in source and binary forms, with or without |
| 22 | ** modification, are permitted provided that the following conditions are |
| 23 | ** met: |
| 24 | ** * Redistributions of source code must retain the above copyright |
| 25 | ** notice, this list of conditions and the following disclaimer. |
| 26 | ** * Redistributions in binary form must reproduce the above copyright |
| 27 | ** notice, this list of conditions and the following disclaimer in |
| 28 | ** the documentation and/or other materials provided with the |
| 29 | ** distribution. |
| 30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
| 31 | ** contributors may be used to endorse or promote products derived |
| 32 | ** from this software without specific prior written permission. |
| 33 | ** |
| 34 | ** |
| 35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
| 46 | ** |
| 47 | ** $QT_END_LICENSE$ |
| 48 | ** |
| 49 | ****************************************************************************/ |
| 50 | |
| 51 | #include "videoplayer.h" |
| 52 | |
| 53 | #include <QtWidgets> |
| 54 | #include <QVideoWidget> |
| 55 | |
| 56 | VideoPlayer::VideoPlayer(QWidget *parent) |
| 57 | : QWidget(parent) |
| 58 | { |
| 59 | m_mediaPlayer = new QMediaPlayer(this, QMediaPlayer::VideoSurface); |
| 60 | QVideoWidget *videoWidget = new QVideoWidget; |
| 61 | |
| 62 | QAbstractButton *openButton = new QPushButton(tr(s: "Open..." )); |
| 63 | connect(sender: openButton, signal: &QAbstractButton::clicked, receiver: this, slot: &VideoPlayer::openFile); |
| 64 | |
| 65 | m_playButton = new QPushButton; |
| 66 | m_playButton->setEnabled(false); |
| 67 | m_playButton->setIcon(style()->standardIcon(standardIcon: QStyle::SP_MediaPlay)); |
| 68 | |
| 69 | connect(sender: m_playButton, signal: &QAbstractButton::clicked, |
| 70 | receiver: this, slot: &VideoPlayer::play); |
| 71 | |
| 72 | m_positionSlider = new QSlider(Qt::Horizontal); |
| 73 | m_positionSlider->setRange(min: 0, max: 0); |
| 74 | |
| 75 | connect(sender: m_positionSlider, signal: &QAbstractSlider::sliderMoved, |
| 76 | receiver: this, slot: &VideoPlayer::setPosition); |
| 77 | |
| 78 | m_errorLabel = new QLabel; |
| 79 | m_errorLabel->setSizePolicy(hor: QSizePolicy::Preferred, ver: QSizePolicy::Maximum); |
| 80 | |
| 81 | QBoxLayout *controlLayout = new QHBoxLayout; |
| 82 | controlLayout->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0); |
| 83 | controlLayout->addWidget(openButton); |
| 84 | controlLayout->addWidget(m_playButton); |
| 85 | controlLayout->addWidget(m_positionSlider); |
| 86 | |
| 87 | QBoxLayout *layout = new QVBoxLayout; |
| 88 | layout->addWidget(videoWidget); |
| 89 | layout->addLayout(layout: controlLayout); |
| 90 | layout->addWidget(m_errorLabel); |
| 91 | |
| 92 | setLayout(layout); |
| 93 | |
| 94 | m_mediaPlayer->setVideoOutput(videoWidget); |
| 95 | connect(sender: m_mediaPlayer, signal: &QMediaPlayer::stateChanged, |
| 96 | receiver: this, slot: &VideoPlayer::mediaStateChanged); |
| 97 | connect(sender: m_mediaPlayer, signal: &QMediaPlayer::positionChanged, receiver: this, slot: &VideoPlayer::positionChanged); |
| 98 | connect(sender: m_mediaPlayer, signal: &QMediaPlayer::durationChanged, receiver: this, slot: &VideoPlayer::durationChanged); |
| 99 | connect(sender: m_mediaPlayer, signal: QOverload<QMediaPlayer::Error>::of(ptr: &QMediaPlayer::error), |
| 100 | receiver: this, slot: &VideoPlayer::handleError); |
| 101 | } |
| 102 | |
| 103 | VideoPlayer::~VideoPlayer() |
| 104 | { |
| 105 | } |
| 106 | |
| 107 | void VideoPlayer::openFile() |
| 108 | { |
| 109 | QFileDialog fileDialog(this); |
| 110 | fileDialog.setAcceptMode(QFileDialog::AcceptOpen); |
| 111 | fileDialog.setWindowTitle(tr(s: "Open Movie" )); |
| 112 | QStringList supportedMimeTypes = m_mediaPlayer->supportedMimeTypes(); |
| 113 | if (!supportedMimeTypes.isEmpty()) |
| 114 | fileDialog.setMimeTypeFilters(supportedMimeTypes); |
| 115 | fileDialog.setDirectory(QStandardPaths::standardLocations(type: QStandardPaths::MoviesLocation).value(i: 0, defaultValue: QDir::homePath())); |
| 116 | if (fileDialog.exec() == QDialog::Accepted) |
| 117 | setUrl(fileDialog.selectedUrls().constFirst()); |
| 118 | } |
| 119 | |
| 120 | void VideoPlayer::setUrl(const QUrl &url) |
| 121 | { |
| 122 | m_errorLabel->setText(QString()); |
| 123 | setWindowFilePath(url.isLocalFile() ? url.toLocalFile() : QString()); |
| 124 | m_mediaPlayer->setMedia(media: url); |
| 125 | m_playButton->setEnabled(true); |
| 126 | } |
| 127 | |
| 128 | void VideoPlayer::play() |
| 129 | { |
| 130 | switch (m_mediaPlayer->state()) { |
| 131 | case QMediaPlayer::PlayingState: |
| 132 | m_mediaPlayer->pause(); |
| 133 | break; |
| 134 | default: |
| 135 | m_mediaPlayer->play(); |
| 136 | break; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | void VideoPlayer::mediaStateChanged(QMediaPlayer::State state) |
| 141 | { |
| 142 | switch(state) { |
| 143 | case QMediaPlayer::PlayingState: |
| 144 | m_playButton->setIcon(style()->standardIcon(standardIcon: QStyle::SP_MediaPause)); |
| 145 | break; |
| 146 | default: |
| 147 | m_playButton->setIcon(style()->standardIcon(standardIcon: QStyle::SP_MediaPlay)); |
| 148 | break; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | void VideoPlayer::positionChanged(qint64 position) |
| 153 | { |
| 154 | m_positionSlider->setValue(position); |
| 155 | } |
| 156 | |
| 157 | void VideoPlayer::durationChanged(qint64 duration) |
| 158 | { |
| 159 | m_positionSlider->setRange(min: 0, max: duration); |
| 160 | } |
| 161 | |
| 162 | void VideoPlayer::setPosition(int position) |
| 163 | { |
| 164 | m_mediaPlayer->setPosition(position); |
| 165 | } |
| 166 | |
| 167 | void VideoPlayer::handleError() |
| 168 | { |
| 169 | m_playButton->setEnabled(false); |
| 170 | const QString errorString = m_mediaPlayer->errorString(); |
| 171 | QString message = "Error: " ; |
| 172 | if (errorString.isEmpty()) |
| 173 | message += " #" + QString::number(int(m_mediaPlayer->error())); |
| 174 | else |
| 175 | message += errorString; |
| 176 | m_errorLabel->setText(message); |
| 177 | } |
| 178 | |