Marble

SoundTrack.cpp
1// SPDX-License-Identifier: LGPL-2.1-or-later
2//
3// SPDX-FileCopyrightText: 2014 Sanjiban Bairagya <sanjiban22393@gmail.com>
4//
5
6#include "SoundTrack.h"
7
8#include "PlaybackSoundCueItem.h"
9
10namespace Marble
11{
12
13SoundTrack::SoundTrack(PlaybackSoundCueItem *item)
14{
15 m_item = item;
16 m_progress = 0;
17 m_delayBeforeTrackStarts = 0;
18 m_paused = true;
19 connect(&m_timer, &QTimer::timeout, this, &SoundTrack::playSlot);
20}
21
22void SoundTrack::setDelayBeforeTrackStarts(double delay)
23{
24 m_delayBeforeTrackStarts = delay;
25 m_timer.setSingleShot(true);
26 m_timer.setInterval(m_delayBeforeTrackStarts * 1000);
27}
28
29double SoundTrack::delayBeforeTrackStarts() const
30{
31 return m_delayBeforeTrackStarts;
32}
33
34void SoundTrack::play()
35{
36 m_paused = false;
37 m_playTime = QDateTime::currentDateTime();
38 if (m_progress <= m_delayBeforeTrackStarts) {
39 m_timer.start((m_delayBeforeTrackStarts - m_progress) * 1000);
40 } else {
41 m_item->play();
42 }
43}
44
45void SoundTrack::playSlot()
46{
47 m_item->play();
48}
49
50void SoundTrack::pause()
51{
52 m_paused = true;
53 m_pauseTime = QDateTime::currentDateTime();
54 m_progress += m_playTime.msecsTo(m_pauseTime);
55 if (m_timer.isActive()) {
56 m_timer.stop();
57 } else {
58 m_item->pause();
59 }
60}
61
62void SoundTrack::seek(double offset)
63{
64 m_timer.stop();
65 m_progress = offset;
66 m_playTime = QDateTime::currentDateTime().addMSecs(-offset * 1000);
67
68 if (offset <= m_delayBeforeTrackStarts) {
69 if (!m_paused) {
70 m_pauseTime = QDateTime();
71 m_item->stop();
72 m_timer.start((m_delayBeforeTrackStarts - m_progress) * 1000);
73 } else {
74 m_pauseTime = QDateTime::currentDateTime();
75 }
76 } else {
77 if (!m_paused) {
78 m_pauseTime = QDateTime();
79 m_item->seek(offset - m_delayBeforeTrackStarts);
80 } else {
81 m_pauseTime = QDateTime::currentDateTime();
82 m_item->seek(offset - m_delayBeforeTrackStarts);
83 }
84 }
85}
86
87void SoundTrack::stop()
88{
89 m_paused = true;
90 m_item->stop();
91 m_timer.stop();
92 m_playTime = QDateTime();
93 m_pauseTime = QDateTime();
94 m_progress = 0;
95}
96
97void SoundTrack::setPaused(bool pause)
98{
99 m_paused = pause;
100}
101
102}
103
104#include "moc_SoundTrack.cpp"
Q_SCRIPTABLE Q_NOREPLY void pause()
Binds a QML item to a specific geodetic location in screen coordinates.
QDateTime addMSecs(qint64 msecs) const const
QDateTime currentDateTime()
qint64 msecsTo(const QDateTime &other) const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void setInterval(int msec)
bool isActive() const const
void setSingleShot(bool singleShot)
void start()
void stop()
void timeout()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 8 2024 12:02:44 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.