on e700c19 help sign in
107 lines 2.4 kB history raw
1/*
2 * Copyright (C) 2013 Canonical, Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Author: Michael Zanetti <michael.zanetti@canonical.com>
17 */
19#include "audio.h"
21Audio::Audio(QObject* parent):
22 QObject(parent),
23 m_playbackState(StoppedState)
25 qsrand(time(NULL));
26 m_timer.setInterval(1000);
27 connect(&m_timer, SIGNAL(timeout()), SLOT(timerEvent()));
30QUrl Audio::source() const
32 return m_source;
35void Audio::setSource(const QUrl &source)
37 if (m_source != source) {
38 m_source = source;
39 Q_EMIT sourceChanged(source);
41 m_position = 0;
42 Q_EMIT positionChanged(m_position);
44 m_duration = (qrand() % 20000) + 10000;
45 Q_EMIT durationChanged(m_duration);
46 }
49Audio::PlaybackState Audio::playbackState() const
51 return m_playbackState;
54int Audio::position() const
56 return m_position;
59int Audio::duration() const
61 return m_duration;
64QString Audio::errorString() const
66 return QString();
69void Audio::pause()
71 if (m_playbackState == PlayingState) {
72 m_playbackState = PausedState;
73 Q_EMIT playbackStateChanged(m_playbackState);
74 m_timer.stop();
75 }
78void Audio::play()
80 if (m_playbackState != PlayingState && m_source.isValid()) {
81 m_playbackState = PlayingState;
82 Q_EMIT playbackStateChanged(m_playbackState);
84 m_timer.start();
85 }
88void Audio::stop()
90 if (m_playbackState != StoppedState) {
91 m_playbackState = StoppedState;
92 Q_EMIT playbackStateChanged(m_playbackState);
93 m_timer.stop();
94 m_position = 0;
95 Q_EMIT positionChanged(m_position);
96 }
99void Audio::timerEvent()
101 if (m_position + 1000 < m_duration) {
102 m_position += 1000;
103 Q_EMIT positionChanged(m_position);
104 } else {
105 stop();
106 }