0 __memmove_ssse3_back /usr/lib/debug/lib/x86_64-linux-gnu/libc-2.19.so 93 0x7ffff397c200 1 QIODevice::read(char *, long long) 0x7ffff4b3731d 2 ?? 0x7fffd5f0a424 3 QMetaObject::activate(QObject *, int, int, void * *) 0x7ffff4c3a36e 4 QTimer::timerEvent(QTimerEvent *) 0x7ffff4c478c8 5 QObject::event(QEvent *) 0x7ffff4c3b3bb 6 QApplicationPrivate::notify_helper(QObject *, QEvent *) 0x7ffff5fc14dc 7 QApplication::notify(QObject *, QEvent *) 0x7ffff5fc6640 8 QCoreApplication::notifyInternal(QObject *, QEvent *) 0x7ffff4c0c6a3 9 QTimerInfoList::activateTimers() 0x7ffff4c609ed 10 ?? 0x7ffff4c60e31 11 g_main_context_dispatch 0x7ffff2239c5d 12 ?? 0x7ffff2239f48 13 g_main_context_iteration 0x7ffff2239ffc 14 QEventDispatcherGlib::processEvents(QFlags<QEventLoop::ProcessEventsFlag>) 0x7ffff4c61ae7 15 QEventLoop::exec(QFlags<QEventLoop::ProcessEventsFlag>) 0x7ffff4c0a252 16 QCoreApplication::exec() 0x7ffff4c11d3d 17 main main.cpp 41 0x4217c4
int main(int argc, char *argv[]){ QCoreApplication a(argc, argv); MainModule* m = new MainModule(); return a.exec();}MainModule::MainModule(){ audio.initialize(); QFile file; file.setFileName("/home/path/to/file/1.wav"); file.open(QIODevice::ReadOnly); QByteArray ba = file.readAll(); audio.playSound(ba);}void Audio::initialize(){ this->outputFormat.setSampleRate(44100); this->outputFormat.setChannelCount(1); this->outputFormat.setSampleSize(16); this->outputFormat.setCodec("audio/pcm"); this->outputFormat.setByteOrder(QAudioFormat::LittleEndian); this->outputFormat.setSampleType(QAudioFormat::SignedInt); QAudioDeviceInfo outputInfo(QAudioDeviceInfo::defaultOutputDevice()); if (!outputInfo.isFormatSupported(outputFormat)){ return; } this->player = new QAudioOutput(this->outputFormat);}void Audio::playSound(QByteArray& sound){ if(this->player != 0){ QByteArray temp = sound; QBuffer buffer(&temp); buffer.open(QIODevice::ReadOnly); this->player->start(&buffer); QObject::connect(this->player, SIGNAL(stateChanged(QAudio::State)), &this->audioLoop, SLOT(quit())); do{ this->audioLoop.exec(); }while(this->player->state() == QAudio::ActiveState && this->player != 0); buffer.close(); }}void Audio::quitLoop(){ if(this->audioLoop.isRunning()) this->audioLoop.quit(); if(this->player != 0) this->player->stop();}
#include <QBuffer>#include <QAudioOutput>#include <QEventLoop>class Audio : public QObject { Q_OBJECTpublic: void initialize(); public slots: void playSound(const QByteArray& sound);public: Audio(); ~Audio(); QAudioOutput* player; QAudioFormat outputFormat; QEventLoop audioLoop; QByteArray outputByteArray; bool initializeAudioOutput(); void quitLoop();};
#include "audio.h"class MainModule : public QObject{ Q_OBJECTpublic: MainModule();private: Audio audio;};