1 /*
2  * Copyright (C) 2008 Matthew Gates
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA  02110-1335, USA.
17  */
18 
19 #include "StelAudioMgr.hpp"
20 #include "StelFileMgr.hpp"
21 #include <QDebug>
22 #include <QDir>
23 
24 #ifdef ENABLE_MEDIA
25 
26 #include <QMediaPlayer>
27 
StelAudioMgr()28 StelAudioMgr::StelAudioMgr()
29 {
30 }
31 
~StelAudioMgr()32 StelAudioMgr::~StelAudioMgr()
33 {
34 	for (const auto& id : audioObjects.keys())
35 	{
36 		dropSound(id);
37 	}
38 }
39 
loadSound(const QString & filename,const QString & id)40 void StelAudioMgr::loadSound(const QString& filename, const QString& id)
41 {
42 	if (audioObjects.contains(id))
43 	{
44 		qWarning() << "Audio object with ID" << id << "already exists, dropping it";
45 		dropSound(id);
46 	}
47 
48 	QMediaPlayer* sound = new QMediaPlayer();
49 	QString path = QFileInfo(filename).absoluteFilePath();
50 	sound->setMedia(QMediaContent(QUrl::fromLocalFile(path)));
51 	audioObjects[id] = sound;
52 }
53 
playSound(const QString & id)54 void StelAudioMgr::playSound(const QString& id)
55 {
56 	if (audioObjects.contains(id))
57 	{
58 		if (audioObjects[id]!=Q_NULLPTR)
59 		{
60 			// if already playing, stop and play from the start
61 			if (audioObjects[id]->state()==QMediaPlayer::PlayingState)
62 				audioObjects[id]->stop();
63 
64 			// otherwise just play it
65 			audioObjects[id]->play();
66 		}
67 		else
68 			qDebug() << "StelAudioMgr: Cannot play sound, " << id << "not correctly loaded.";
69 	}
70 	else
71 		qDebug() << "StelAudioMgr: Cannot play sound, " << id << "not loaded.";
72 }
73 
pauseSound(const QString & id)74 void StelAudioMgr::pauseSound(const QString& id)
75 {
76 	if (audioObjects.contains(id))
77 	{
78 		if (audioObjects[id]!=Q_NULLPTR)
79 			audioObjects[id]->pause();
80 		else
81 			qDebug() << "StelAudioMgr: Cannot pause sound, " << id << "not correctly loaded.";
82 	}
83 	else
84 		qDebug() << "StelAudioMgr: Cannot pause sound, " << id << "not loaded.";
85 }
86 
stopSound(const QString & id)87 void StelAudioMgr::stopSound(const QString& id)
88 {
89 	if (audioObjects.contains(id))
90 	{
91 		if (audioObjects[id]!=Q_NULLPTR)
92 			audioObjects[id]->stop();
93 		else
94 			qDebug() << "StelAudioMgr: Cannot stop sound, " << id << "not correctly loaded.";
95 	}
96 	else
97 		qDebug() << "StelAudioMgr: Cannot stop sound, " << id << "not loaded.";
98 }
99 
dropSound(const QString & id)100 void StelAudioMgr::dropSound(const QString& id)
101 {
102 	if (!audioObjects.contains(id))
103 	{
104 		qDebug() << "StelAudioMgr: Cannot drop sound, " << id << "not loaded.";
105 		return;
106 	}
107 	if (audioObjects[id]!=Q_NULLPTR)
108 	{
109 		audioObjects[id]->stop();
110 		delete audioObjects[id];
111 		audioObjects.remove(id);
112 	}
113 }
114 
115 
position(const QString & id)116 qint64 StelAudioMgr::position(const QString& id)
117 {
118 	if (!audioObjects.contains(id))
119 	{
120 		qDebug() << "StelAudioMgr: Cannot report position for sound, " << id << "not loaded.";
121 		return(-1);
122 	}
123 	if (audioObjects[id]!=Q_NULLPTR)
124 	{
125 		return audioObjects[id]->position();
126 	}
127 	return (-1);
128 }
129 
duration(const QString & id)130 qint64 StelAudioMgr::duration(const QString& id)
131 {
132 	if (!audioObjects.contains(id))
133 	{
134 		qDebug() << "StelAudioMgr: Cannot report duration for sound, " << id << "not loaded.";
135 		return(-1);
136 	}
137 	if (audioObjects[id]!=Q_NULLPTR)
138 	{
139 		return audioObjects[id]->duration();
140 	}
141 	return (-1);
142 }
143 
144 #else
loadSound(const QString & filename,const QString & id)145 void StelAudioMgr::loadSound(const QString& filename, const QString& id)
146 {
147 	qWarning() << "This build of Stellarium does not support sound - cannot load audio" << QDir::toNativeSeparators(filename) << id;
148 }
StelAudioMgr()149 StelAudioMgr::StelAudioMgr() {}
~StelAudioMgr()150 StelAudioMgr::~StelAudioMgr() {;}
playSound(const QString &)151 void StelAudioMgr::playSound(const QString&) {;}
pauseSound(const QString &)152 void StelAudioMgr::pauseSound(const QString&) {;}
stopSound(const QString &)153 void StelAudioMgr::stopSound(const QString&) {;}
dropSound(const QString &)154 void StelAudioMgr::dropSound(const QString&) {;}
position(const QString &)155 qint64 StelAudioMgr::position(const QString&){return -1;}
duration(const QString &)156 qint64 StelAudioMgr::duration(const QString&){return -1;}
157 #endif
158