on a628251 help sign in
132 lines 2.8 kB history raw
1/*
2 * Copyright (C) 2013-2016 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),
24 m_playlist(nullptr)
26 qsrand(time(nullptr));
27 m_timer.setInterval(1000);
28 connect(&m_timer, &QTimer::timeout, this, &Audio::timerEvent);
30 m_position = 0;
31 Q_EMIT positionChanged(m_position);
33 m_duration = (qrand() % 20000) + 10000;
34 Q_EMIT durationChanged(m_duration);
37QUrl Audio::source() const
39 return m_source;
42void Audio::setSource(const QUrl &source)
44 if (m_source != source) {
45 m_source = source;
46 Q_EMIT sourceChanged(source);
47 }
50Audio::PlaybackState Audio::playbackState() const
52 return m_playbackState;
55int Audio::position() const
57 return m_position;
60int Audio::duration() const
62 return m_duration;
65QString Audio::errorString() const
67 return QString();
70void Audio::pause()
72 if (m_playbackState == PlayingState) {
73 m_playbackState = PausedState;
74 Q_EMIT playbackStateChanged(m_playbackState);
75 m_timer.stop();
76 }
79void Audio::play()
81 if (m_playbackState != PlayingState) {
82 m_playbackState = PlayingState;
83 Q_EMIT playbackStateChanged(m_playbackState);
85 m_timer.start();
86 }
89void Audio::stop()
91 if (m_playbackState != StoppedState) {
92 m_playbackState = StoppedState;
93 Q_EMIT playbackStateChanged(m_playbackState);
94 m_timer.stop();
95 m_position = 0;
96 Q_EMIT positionChanged(m_position);
97 }
100void Audio::timerEvent()
102 if (m_position + 1000 < m_duration) {
103 m_position += 1000;
104 Q_EMIT positionChanged(m_position);
105 } else {
106 stop();
107 }
110Audio::AudioRole Audio::audioRole() const
112 return Audio::MusicRole;
115void Audio::setAudioRole(Audio::AudioRole audioRole)
117 Q_UNUSED(audioRole);
120DeclarativePlaylist *Audio::playlist() const
122 return m_playlist;
125void Audio::setPlaylist(DeclarativePlaylist *playlist)
127 if (playlist == m_playlist)
128 return;
130 m_playlist = playlist;
131 Q_EMIT playlistChanged();