2019-10-08 14:33:38 -04:00
|
|
|
#ifndef NOTIFICATIONAUDIO_H
|
|
|
|
#define NOTIFICATIONAUDIO_H
|
|
|
|
|
|
|
|
#include <QIODevice>
|
|
|
|
#include <QBuffer>
|
|
|
|
#include <QAudioDecoder>
|
|
|
|
#include <QAudioDeviceInfo>
|
|
|
|
#include <QAudioFormat>
|
|
|
|
#include <QAudioOutput>
|
|
|
|
#include <QFile>
|
|
|
|
|
|
|
|
// Class for decode audio files like MP3 and push decoded audio data to QOutputDevice (like speaker) and also signal newData().
|
|
|
|
// For decoding it uses QAudioDecoder which uses QAudioFormat for decode audio file for desire format, then put decoded data to buffer.
|
|
|
|
// based on: https://github.com/Znurre/QtMixer
|
|
|
|
class NotificationAudio : public QIODevice
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
NotificationAudio(QObject * parent=nullptr);
|
2019-10-08 21:26:17 -04:00
|
|
|
~NotificationAudio();
|
2019-10-08 14:33:38 -04:00
|
|
|
|
|
|
|
bool isInitialized() const { return m_init; }
|
|
|
|
|
|
|
|
enum State { Playing, Stopped };
|
|
|
|
|
|
|
|
bool atEnd() const override;
|
|
|
|
|
|
|
|
public slots:
|
|
|
|
void init(const QAudioDeviceInfo &device, const QAudioFormat& format);
|
|
|
|
void play(const QString &filePath);
|
|
|
|
void stop();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
qint64 readData(char* data, qint64 maxlen) override;
|
|
|
|
qint64 writeData(const char* data, qint64 len) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
State m_state;
|
|
|
|
QBuffer m_input;
|
|
|
|
QBuffer m_output;
|
|
|
|
QByteArray m_data;
|
|
|
|
QAudioFormat m_format;
|
|
|
|
QAudioDeviceInfo m_device;
|
|
|
|
QAudioDecoder * m_decoder;
|
|
|
|
QAudioOutput * m_audio;
|
|
|
|
|
|
|
|
bool m_init;
|
|
|
|
bool m_isDecodingFinished;
|
|
|
|
|
|
|
|
void resetBuffers();
|
|
|
|
|
|
|
|
private slots:
|
|
|
|
void bufferReady();
|
|
|
|
void finished();
|
2019-10-11 23:34:31 -04:00
|
|
|
void errored(QAudioDecoder::Error);
|
2019-10-08 14:33:38 -04:00
|
|
|
|
|
|
|
signals:
|
|
|
|
void initialized();
|
|
|
|
void stateChanged(NotificationAudio::State state);
|
|
|
|
void newData(const QByteArray& data);
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // NOTIFICATIONAUDIO_H
|