Small optimization to media player. Web page updated.
[jspeed] / src / mediaplayer.cpp
1 /*
2  * This file is part of jSpeed.
3  *
4  * jSpeed 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, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * jSpeed 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 jSpeed.  If not, see <http://www.gnu.org/licenses/>.
16  *
17  */
18
19 #include <QtCore/QUrl>
20 #include <QtCore/QDir>
21 #include <QMediaPlayer>
22 #include <QMediaContent>
23 #include "mediaplayer.h"
24 #include "settings.h"
25
26 namespace
27 {
28     QString const FORMATS[MediaPlayer::FORMAT_COUNT] = {"mp3", "aac", "wav"};
29     QString const LOCAL_SOUND_DIR = ":/resources/sounds/";
30 }
31
32 bool MediaPlayer::play(QString const& file)
33 {
34     static QMediaPlayer player;
35
36     QMediaContent media(QUrl::fromLocalFile(file));
37
38     if(media.isNull())
39     {
40         return false;
41     }
42
43     player.setMedia(media);
44     player.play();
45     return true;
46 }
47
48 void MediaPlayer::getSupportedFormats(QList<QString>& formats)
49 {
50     formats.clear();
51
52     for(int i = 0; i < FORMAT_COUNT; i++)
53     {
54         formats.push_back(FORMATS[i]);
55     }
56 }
57
58 QString MediaPlayer::getFormatPattern()
59 {
60    QString result = "";
61
62    for(int i = 0; i < FORMAT_COUNT; i++)
63    {
64        result += "*." + FORMATS[i] + " ";
65    }
66
67    return result.trimmed();
68 }
69
70 QStringList MediaPlayer::getFormatPatterns()
71 {
72     QStringList result;
73
74     for(int i = 0; i < FORMAT_COUNT; i++)
75     {
76         result.push_back("*." + FORMATS[i]);
77     }
78
79     return result;
80 }
81
82 QString MediaPlayer::getSoundDir()
83 {
84     return Settings::getDir() + "sounds" + QDir::separator();
85 }
86
87 QString const& MediaPlayer::getLocalSoundDir()
88 {
89     return LOCAL_SOUND_DIR;
90 }
91