Fixed some error handling for notifications
This commit is contained in:
		
							parent
							
								
									e3439fd4af
								
							
						
					
					
						commit
						b3df2985b7
					
				| @ -33,6 +33,7 @@ void NotificationAudio::init(const QAudioDeviceInfo &device, const QAudioFormat& | |||||||
|     if(!m_decoder){ |     if(!m_decoder){ | ||||||
|         m_decoder = new QAudioDecoder(this); |         m_decoder = new QAudioDecoder(this); | ||||||
|         connect(m_decoder, &QAudioDecoder::bufferReady, this, &NotificationAudio::bufferReady); |         connect(m_decoder, &QAudioDecoder::bufferReady, this, &NotificationAudio::bufferReady); | ||||||
|  |         connect(m_decoder, static_cast<void(QAudioDecoder::*)(QAudioDecoder::Error)>(&QAudioDecoder::error), this, &NotificationAudio::errored); | ||||||
|         connect(m_decoder, &QAudioDecoder::finished, this, &NotificationAudio::finished); |         connect(m_decoder, &QAudioDecoder::finished, this, &NotificationAudio::finished); | ||||||
|     } |     } | ||||||
|     m_decoder->setAudioFormat(m_format); |     m_decoder->setAudioFormat(m_format); | ||||||
| @ -66,21 +67,11 @@ void NotificationAudio::playFile(const QString &filePath){ | |||||||
| 
 | 
 | ||||||
|     resetBuffers(); |     resetBuffers(); | ||||||
| 
 | 
 | ||||||
|     m_file.setFileName(filePath); |  | ||||||
|     if (!m_file.open(QFile::ReadOnly)){ |  | ||||||
|         return; |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     if(!m_file.isReadable()){ |  | ||||||
|         return; |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     m_decoder->setSourceDevice(&m_file); |  | ||||||
|     m_decoder->start(); |  | ||||||
| 
 |  | ||||||
|     m_state = State::Playing; |     m_state = State::Playing; | ||||||
|     emit stateChanged(m_state); |     emit stateChanged(m_state); | ||||||
| 
 | 
 | ||||||
|  |     m_decoder->setSourceFilename(filePath); | ||||||
|  |     m_decoder->start(); | ||||||
|     m_audio->start(this); |     m_audio->start(this); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| @ -94,7 +85,6 @@ void NotificationAudio::stop() { | |||||||
| // Reset the internal buffers and ensure the decoder and audio device is stopped
 | // Reset the internal buffers and ensure the decoder and audio device is stopped
 | ||||||
| void NotificationAudio::resetBuffers() { | void NotificationAudio::resetBuffers() { | ||||||
|     if(m_audio){ |     if(m_audio){ | ||||||
| 
 |  | ||||||
|         if(m_audio->state() != QAudio::SuspendedState){ |         if(m_audio->state() != QAudio::SuspendedState){ | ||||||
|             m_audio->suspend(); |             m_audio->suspend(); | ||||||
|         } |         } | ||||||
| @ -106,10 +96,6 @@ void NotificationAudio::resetBuffers() { | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     if(m_file.isOpen()){ |  | ||||||
|         m_file.close(); |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     m_data.clear(); |     m_data.clear(); | ||||||
|     m_isDecodingFinished = false; |     m_isDecodingFinished = false; | ||||||
| } | } | ||||||
| @ -156,6 +142,9 @@ bool NotificationAudio::atEnd() const { | |||||||
| // handle buffered data ready
 | // handle buffered data ready
 | ||||||
| void NotificationAudio::bufferReady() { | void NotificationAudio::bufferReady() { | ||||||
|     const QAudioBuffer &buffer = m_decoder->read(); |     const QAudioBuffer &buffer = m_decoder->read(); | ||||||
|  |     if(!buffer.isValid()){ | ||||||
|  |         return; | ||||||
|  |     } | ||||||
| 
 | 
 | ||||||
|     const int length = buffer.byteCount(); |     const int length = buffer.byteCount(); | ||||||
|     const char *data = buffer.constData<char>(); |     const char *data = buffer.constData<char>(); | ||||||
| @ -167,3 +156,8 @@ void NotificationAudio::bufferReady() { | |||||||
| void NotificationAudio::finished() { | void NotificationAudio::finished() { | ||||||
|     m_isDecodingFinished = true; |     m_isDecodingFinished = true; | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | // handle buffered data decoding error
 | ||||||
|  | void NotificationAudio::errored(QAudioDecoder::Error /*error*/) { | ||||||
|  |     stop(); | ||||||
|  | } | ||||||
|  | |||||||
| @ -37,7 +37,6 @@ protected: | |||||||
|     qint64 writeData(const char* data, qint64 len) override; |     qint64 writeData(const char* data, qint64 len) override; | ||||||
| 
 | 
 | ||||||
| private: | private: | ||||||
|     QFile m_file; |  | ||||||
|     State m_state; |     State m_state; | ||||||
|     QBuffer m_input; |     QBuffer m_input; | ||||||
|     QBuffer m_output; |     QBuffer m_output; | ||||||
| @ -56,6 +55,7 @@ private: | |||||||
| private slots: | private slots: | ||||||
|     void bufferReady(); |     void bufferReady(); | ||||||
|     void finished(); |     void finished(); | ||||||
|  |     void errored(QAudioDecoder::Error); | ||||||
| 
 | 
 | ||||||
| signals: | signals: | ||||||
|     void initialized(); |     void initialized(); | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user
	 Jordan Sherer
						Jordan Sherer