Skip to content
Browse files

Extra audio features, loop audio & close all but currently playing

  • Loading branch information...
1 parent b6866de commit 9af09ee7a8f79546b67801ae2f579ad60a00cbef @ItalianSunglassMovie committed Apr 24, 2015
View
86 gui/audiostream.cpp
@@ -0,0 +1,86 @@
+#include "audiostream.h"
+
+AudioStream::AudioStream(QMap<QString, AudioStream*> *audio) :
+ QMediaPlayer(new QMediaPlayer()),
+ audio(audio)
+{
+}
+
+AudioStream::~AudioStream()
+{
+}
+
+void AudioStream::updateFlags(QString flags)
+{
+ if(m_flags == flags)
+ return;
+
+ m_flags = flags;
+
+ if (flags.contains("!") || flags.contains("only"))
+ only_play_this = true;
+ else
+ only_play_this = false;
+
+ if (flags.contains("@") || flags.contains("loop"))
+ {
+ if(loop == false)
+ {
+ loop = true;
+ connect(this, SIGNAL(stateChanged(QMediaPlayer::State)), this, SLOT(playAgain(QMediaPlayer::State)));
+ }
+ }
+ else
+ {
+ if(loop == true)
+ {
+ loop = false;
+ disconnect(this, SIGNAL(stateChanged(QMediaPlayer::State)), this, SLOT(playAgain(QMediaPlayer::State)));
+ }
+ }
+}
+
+void AudioStream::play()
+{
+ if(only_play_this)
+ {
+ for(AudioStream *x : *audio)
+ {
+ if(x != this)
+ {
+ if(x->state() != StoppedState)
+ x->stop();
+ }
+ }
+ }
+ stop_looping = false;
+ (static_cast<QMediaPlayer*>(this))->play();
+}
+
+void AudioStream::stop()
+{
+ stop_looping = true;
+ QPropertyAnimation *fadeout = new QPropertyAnimation(this, "volume");
+ fadeout->setDuration(500);
+ fadeout->setStartValue(this->volume());
+ fadeout->setEndValue(0);
+ connect(fadeout, SIGNAL(finished()), this, SLOT(stopStream()));
+ fadeout->start(QPropertyAnimation::DeleteWhenStopped);
+}
+
+void AudioStream::playAgain(State state)
+{
+ if (state == StoppedState)
+ {
+ if(!stop_looping)
+ {
+ play();
+ }
+ }
+}
+
+void AudioStream::stopStream()
+{
+ (static_cast<QMediaPlayer*>(this))->stop();
+}
+
View
31 gui/audiostream.h
@@ -0,0 +1,31 @@
+#ifndef AUDIOSTREAM_H
+#define AUDIOSTREAM_H
+#include <QMediaPlayer>
+#include <QMap>
+#include <QPropertyAnimation>
+
+class AudioStream : public QMediaPlayer
+{
+ Q_OBJECT
+public:
+ AudioStream(QMap<QString, AudioStream*> *audio);
+ ~AudioStream();
+ bool loop = false;
+ bool only_play_this = false;
+ bool stop_looping = false;
+ void updateFlags(QString flags);
+
+public slots:
+ void play();
+ void stop();
+
+private slots:
+ void playAgain(QMediaPlayer::State);
+ void stopStream();
+
+private:
+ QMap<QString, AudioStream*> *audio;
+ QString m_flags;
+};
+
+#endif // AUDIOSTREAM_H
View
11 gui/fastqspwindow.cpp
@@ -264,7 +264,7 @@ void FastQSPWindow::linkClicked(const QUrl &url) {
loadPage();
}
-void FastQSPWindow::playAudio(QString filename, int vol) {
+void FastQSPWindow::playAudio(QString filename, int vol, QString flags) {
filename = filename.replace('\\', '/');
#if QT_VERSION < 0x050000
if (QFile(filename).exists() && media->state() != Phonon::PlayingState) {
@@ -280,7 +280,8 @@ void FastQSPWindow::playAudio(QString filename, int vol) {
if(audio[filename] == NULL || audio[filename]->state() != QMediaPlayer::PlayingState)
{
if (audio[filename] == NULL)
- audio[filename] = new QMediaPlayer();
+ audio[filename] = new AudioStream(&audio);
+ audio[filename]->updateFlags(flags);
audio[filename]->setMedia(QUrl::fromLocalFile(QFileInfo(filename).absoluteFilePath()));
audio[filename]->setVolume(vol);
audio[filename]->play();
@@ -295,8 +296,10 @@ void FastQSPWindow::stopAudio(QString filename) {
#else
if(filename == NULL)
{
- for(QMediaPlayer *x : audio)
- x->stop();
+ for(AudioStream *x : audio)
+ {
+ x->stop();
+ }
return;
}
if(audio[filename] != NULL)
View
5 gui/fastqspwindow.h
@@ -36,6 +36,7 @@
#include <QMediaPlayer>
#include <QMediaPlaylist>
#include <QMap>
+#include "audiostream.h"
#endif
class FastQSPWindow : public QMainWindow {
@@ -44,7 +45,7 @@ class FastQSPWindow : public QMainWindow {
explicit FastQSPWindow(QWidget *parent = 0);
~FastQSPWindow();
void openFile(const QString &filename);
- void playAudio(QString filename, int vol);
+ void playAudio(QString filename, int vol, QString flags);
void stopAudio(QString filename);
void refreshView();
int getTimeFromStart();
@@ -94,7 +95,7 @@ private slots:
Phonon::AudioOutput *audioOutput;
#else
QMediaPlayer *player;
- QMap<QString, QMediaPlayer*> audio;
+ QMap<QString, AudioStream*> audio;
#endif
QTime timer;
QDir saveDir;
View
6 gui/gui.pro
@@ -19,15 +19,17 @@ SOURCES += main.cpp\
qsp_callback.cpp \
fastqspwindow.cpp \
qsp_htmlbuilder.cpp \
- local_requsts_proxy.cpp
+ local_requsts_proxy.cpp \
+ audiostream.cpp
HEADERS += \
qsp.h \
qsp_default.h \
qsp_callback.h \
fastqspwindow.h \
qsp_htmlbuilder.h \
- local_requsts_proxy.h
+ local_requsts_proxy.h \
+ audiostream.h
CONFIG(release, debug|release) {
BUILDDIR = ../build/release
View
4 gui/qsp_callback.cpp
@@ -42,8 +42,8 @@ QSP_BOOL QSPCallback::isPlayingFile(const QSP_CHAR *file) {
return true;
}
-void QSPCallback::playFile(const QSP_CHAR *file, int volume) {
- qspWin->playAudio(QString::fromWCharArray(file), volume);
+void QSPCallback::playFile(const QSP_CHAR *file, int volume, const QSP_CHAR *flags) {
+ qspWin->playAudio(QString::fromWCharArray(file), volume, QString::fromWCharArray(flags));
qDebug() << "playFile()"
<< ", file: " << QString::fromWCharArray(file)
<< ", volume: " << volume;
View
2 gui/qsp_callback.h
@@ -12,7 +12,7 @@ namespace QSPCallback {
void QSPCallback();
void debug(const QSP_CHAR *str);
QSP_BOOL isPlayingFile(const QSP_CHAR *file);
-void playFile(const QSP_CHAR *file, int volume);
+void playFile(const QSP_CHAR *file, int volume, const QSP_CHAR *flags);
void closeFile(const QSP_CHAR *file);
void showImage(const QSP_CHAR *file);
void showWindow(int type, QSP_BOOL isShow);
View
4 qsp/bindings/default/default_callbacks.cpp
@@ -164,12 +164,12 @@ void qspCallShowWindow(int type, QSP_BOOL isShow) {
}
}
-void qspCallPlayFile(QSP_CHAR *file, int volume) {
+void qspCallPlayFile(QSP_CHAR *file, int volume, const QSP_CHAR *flags) {
/* Здесь начинаем воспроизведение файла с заданной громкостью */
QSPCallState state;
if (qspCallBacks[QSP_CALL_PLAYFILE]) {
qspSaveCallState(&state, QSP_TRUE, QSP_FALSE);
- qspCallBacks[QSP_CALL_PLAYFILE](file, volume);
+ qspCallBacks[QSP_CALL_PLAYFILE](file, volume, flags);
qspRestoreCallState(&state);
}
}
View
2 qsp/callbacks.h
@@ -53,7 +53,7 @@ void qspCallShowMessage(QSP_CHAR *);
int qspCallShowMenu();
void qspCallShowPicture(QSP_CHAR *);
void qspCallShowWindow(int, QSP_BOOL);
-void qspCallPlayFile(QSP_CHAR *, int);
+void qspCallPlayFile(QSP_CHAR *, int, const QSP_CHAR *flags);
QSP_BOOL qspCallIsPlayingFile(QSP_CHAR *);
void qspCallSleep(int);
int qspCallGetMSCount();
View
1 qsp/game.cpp
@@ -237,6 +237,7 @@ void qspOpenQuestFromData(char *data, int dataSize, QSP_CHAR *fileName,
}
if (!isAddLocs)
crc = qspCRC(data, dataSize);
+
count = qspSplitGameStr(data, isUCS2 = !data[1], QSP_STRSDELIM, &strs);
if (!qspCheckQuest(strs, count, isUCS2)) {
qspSetError(QSP_ERR_CANTLOADFILE);
View
14 qsp/playlist.cpp
@@ -19,11 +19,12 @@
#include "callbacks.h"
#include "game.h"
#include "text.h"
+#include <QDebug>
QSP_CHAR *qspPLFiles[QSP_MAXPLFILES];
int qspPLFilesCount = 0;
-static void qspPlayFile(QSP_CHAR *, int, QSP_BOOL);
+static void qspPlayFile(QSP_CHAR *, int, QSP_BOOL, const QSP_CHAR *flags);
static int qspSearchPlayList(QSP_CHAR *);
void qspClearPlayList(QSP_BOOL isFirst) {
@@ -35,7 +36,7 @@ void qspClearPlayList(QSP_BOOL isFirst) {
qspPLFilesCount = 0;
}
-static void qspPlayFile(QSP_CHAR *s, int volume, QSP_BOOL isAddToPlayList) {
+static void qspPlayFile(QSP_CHAR *s, int volume, QSP_BOOL isAddToPlayList, const QSP_CHAR *flags) {
int len;
QSP_CHAR buf[4], *file;
if (!qspIsAnyString(s))
@@ -45,7 +46,7 @@ static void qspPlayFile(QSP_CHAR *s, int volume, QSP_BOOL isAddToPlayList) {
else if (volume > 100)
volume = 100;
file = qspGetAbsFromRelPath(s);
- qspCallPlayFile(file, volume);
+ qspCallPlayFile(file, volume, flags);
free(file);
if (isAddToPlayList) {
if (qspPLFilesCount == QSP_MAXPLFILES) {
@@ -98,10 +99,10 @@ void qspPlayPLFiles() {
pos = qspStrChar(qspPLFiles[i], QSP_PLVOLUMEDELIM[0]);
if (pos) {
*pos = 0;
- qspPlayFile(qspPLFiles[i], qspStrToNum(pos + 1, nullptr), QSP_FALSE);
+ qspPlayFile(qspPLFiles[i], qspStrToNum(pos + 1, nullptr), QSP_FALSE, L"");
*pos = QSP_PLVOLUMEDELIM[0];
} else
- qspPlayFile(qspPLFiles[i], 100, QSP_FALSE);
+ qspPlayFile(qspPLFiles[i], 100, QSP_FALSE, L"");
}
}
@@ -134,7 +135,8 @@ void qspRefreshPlayList() {
QSP_BOOL qspStatementPlayFile(QSPVariant *args, int count, QSP_CHAR **jumpTo,
int extArg) {
int volume = (count == 2 ? QSP_NUM(args[1]) : 100);
- qspPlayFile(QSP_STR(args[0]), volume, QSP_TRUE);
+ const QSP_CHAR *flags = (count == 3 ? QSP_STR(args[2]) : L"");
+ qspPlayFile(QSP_STR(args[0]), volume, QSP_TRUE, flags);
return QSP_FALSE;
}
View
2 qsp/qsp.h
@@ -63,7 +63,7 @@ enum { QSP_WIN_ACTS, QSP_WIN_OBJS, QSP_WIN_VARS, QSP_WIN_INPUT };
enum {
QSP_CALL_DEBUG, /* void func(const QSP_CHAR *str) */
QSP_CALL_ISPLAYINGFILE, /* QSP_BOOL func(const QSP_CHAR *file) */
- QSP_CALL_PLAYFILE, /* void func(const QSP_CHAR *file, int volume) */
+ QSP_CALL_PLAYFILE, /* void func(const QSP_CHAR *file, int volume, const QSP_CHAR *flags) */
QSP_CALL_CLOSEFILE, /* void func(const QSP_CHAR *file) */
QSP_CALL_SHOWIMAGE, /* void func(const QSP_CHAR *file) */
QSP_CALL_SHOWWINDOW, /* void func(int type, QSP_BOOL isShow) */
View
2 qsp/statements.cpp
@@ -178,7 +178,7 @@ void qspInitStats() {
qspAddStatement(qspStatMsg, 0, qspStatementMsg, 1, 1, 1);
qspAddStatement(qspStatOpenGame, 0, qspStatementOpenGame, 0, 1, 1);
qspAddStatement(qspStatOpenQst, 0, qspStatementOpenQst, 1, 1, 1);
- qspAddStatement(qspStatPlay, 0, qspStatementPlayFile, 1, 2, 1, 2);
+ qspAddStatement(qspStatPlay, 0, qspStatementPlayFile, 1, 3, 1, 2, 1);
qspAddStatement(qspStatRefInt, 0, qspStatementRefInt, 0, 0);
qspAddStatement(qspStatSaveGame, 0, qspStatementSaveGame, 0, 1, 1);
qspAddStatement(qspStatSetTimer, 0, qspStatementSetTimer, 1, 1, 2);

0 comments on commit 9af09ee

Please sign in to comment.
Something went wrong with that request. Please try again.