File indexing completed on 2024-04-28 15:51:59

0001 /*
0002     SPDX-FileCopyrightText: 2012 Tobias Koening <tokoe@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 #include "snapshottaker.h"
0007 
0008 #if HAVE_PHONON
0009 
0010 #include <phonon/mediaobject.h>
0011 #include <phonon/videowidget.h>
0012 
0013 #include <QImage>
0014 
0015 SnapshotTaker::SnapshotTaker(const QUrl &url, QObject *parent)
0016     : QObject(parent)
0017     , m_player(new Phonon::VideoPlayer(Phonon::NoCategory, nullptr))
0018 {
0019     m_player->load(url);
0020     m_player->hide();
0021 
0022     connect(m_player->mediaObject(), &Phonon::MediaObject::stateChanged, this, &SnapshotTaker::stateChanged);
0023 
0024     m_player->play();
0025 }
0026 
0027 SnapshotTaker::~SnapshotTaker()
0028 {
0029     m_player->stop();
0030     delete m_player;
0031 }
0032 
0033 void SnapshotTaker::stateChanged(Phonon::State newState, Phonon::State)
0034 {
0035     if (newState == Phonon::PlayingState) {
0036         const QImage image = m_player->videoWidget()->snapshot();
0037         if (!image.isNull()) {
0038             Q_EMIT finished(image);
0039         }
0040 
0041         m_player->stop();
0042         deleteLater();
0043     }
0044 }
0045 #endif