独立线程中使用QSoundEffect播放音频文件

2 篇文章 0 订阅

独立线程中使用QSoundEffect播放音频文件

遇到的问题

主线程中循环检测设备状态,存在某一个时间点或者间隔非常近的时候多个点报警的情况。报警后主线程中继续存在计算任务。
直接使用QSoundEffect可以播放声音,但是声音出现断续的情况。
特此考虑将QSoundEffect的播放工作放到单独线程中。

解决思路

直接将QSoundEffect使用moveToThread()函数,移动到独立线程中,通过信号的方式,响应play()

示例代码

audiothread.h:

#ifndef DLTHREAD_H
#define DLTHREAD_H

#include <QtCore>
#include <QObject>

#include <QSoundEffect>
#include <QThread>

enum VOICE_TYPE{
    VOICE_POSITIVE,
    VOICE_NEGATIVE,
    VOICE_TEMPERATURE
};

class AudioThread : public QObject
{
    Q_OBJECT
public:
    AudioThread(QObject *parent = nullptr);
    ~AudioThread();

signals:
    void doPlay();

public slots:
    void play();
    void play(int type);

private:
    int voice_type;

    QSoundEffect effect;

    QThread thread;

};

#endif // DLTHREAD_H

audiothread.cpp:

#include "audiothread.h"
#include <QUrl>

AudioThread::AudioThread(QObject *parent) :
    QObject(parent)
{
    effect.moveToThread(&thread);
    connect(this, SIGNAL(doPlay()), &effect, SLOT(play()));
    thread.start();
}

AudioThread::~AudioThread()
{
    if(thread.isRunning())
    {
        thread.terminate();
    }
}

void AudioThread::play()
{

    if(!effect.isPlaying())
    {
        effect.setSource(QUrl::fromLocalFile("./voice/temp.wav"));
        effect.setLoopCount(1);

        emit this->doPlay();
    }
}

void AudioThread::play(int type)
{
    if(effect.isPlaying())
    {
        return;
    }

    voice_type = type;
    QString fileName;
    switch (voice_type)
    {
    case VOICE_POSITIVE:
    {
        fileName = "positive.wav";
    }
        break;
    case VOICE_NEGATIVE:
    {
        fileName = "negative.wav";
    }
        break;
    case VOICE_TEMPERATURE:
    {
        fileName = "temp.wav";
    }
        break;
    default:
    {
        return;
    }
    }

    effect.setSource(QUrl::fromLocalFile("./voice/"+fileName));
    effect.setLoopCount(1);
    effect.setVolume(1.0f);

    emit this->doPlay();
}

其他任何文件中:

AudioThread audio;
audio.play();
//audio.play(VOICE_POSITIVE);
  • 2
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
可以使用Qt的QDir类来获取文件夹的所有文件,然后使用线程来读取每个文件的内容。下面是一个简单的示例代码: ```c++ #include <QCoreApplication> #include <QThread> #include <QDir> #include <QStringList> #include <QFile> #include <QDebug> class FileThread : public QThread { public: FileThread(const QString& fileName) : m_fileName(fileName) {} protected: virtual void run() { QFile file(m_fileName); if (file.open(QIODevice::ReadOnly)) { QByteArray data = file.readAll(); qDebug() << m_fileName << "content:" << data; file.close(); } else { qDebug() << "Failed to open " << m_fileName; } } private: QString m_fileName; }; int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QString dirPath = "/path/to/folder"; QDir dir(dirPath); QStringList filters; filters << "*.txt"; // 只读取txt文件 QStringList fileNames = dir.entryList(filters, QDir::Files); QList<FileThread*> threads; foreach (const QString& fileName, fileNames) { FileThread* thread = new FileThread(dir.absoluteFilePath(fileName)); threads.append(thread); thread->start(); } foreach (FileThread* thread, threads) { thread->wait(); delete thread; } return a.exec(); } ``` 上面的代码,FileThread类继承自QThread类,用于读取单个文件。在main函数,首先获取文件夹所有的txt文件名,然后遍历文件名列表,创建FileThread线程对象并启动线程。最后,等待所有线程结束并释放线程对象。注意在读取文件时要进行错误处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值