soundplayer.cpp 2.84 KB
#include "soundplayer.h"
#include "unistd.h"

namespace {
QThread playThread;
}

SoundPlayWorker::SoundPlayWorker()
{
    repeat_ = false;
    proc = new QProcess(this);

    connect(proc, SIGNAL(finished(int)), SLOT(onFinished()));
}

void SoundPlayWorker::play(const QString &filename)
{
    if (proc->state() != QProcess::NotRunning)
    {
        proc->terminate();
        proc->waitForFinished();
    }

    proc->start(QString("aplay"), QStringList(filename));
    proc->waitForStarted();
}

void SoundPlayWorker::playClick()
{
    play("/falinux/sounds/button.wav");
}

void SoundPlayWorker::setVolume(int volume)
{
}

void SoundPlayWorker::repeat(const QString &filename)
{
    repeat_ = true;
    play(filename);
}

void SoundPlayWorker::stop()
{
    repeat_ = false;
    if (proc->state() != QProcess::NotRunning)
    {
        proc->terminate();
        proc->waitForFinished();
    }
}

void SoundPlayWorker::onFinished()
{
    if (repeat_)
        proc->start();
}

SoundPlayer *SoundPlayer::instance = 0;
SoundPlayer::SoundPlayer()
{
    instance = this;

    SoundPlayWorker *w = new SoundPlayWorker;
    w->moveToThread(&playThread);
    connect(this, SIGNAL(setVolume(int)), w, SLOT(setVolume(int)));
    connect(this, SIGNAL(operate(QString)), w, SLOT(play(QString)));
    connect(this, SIGNAL(click()), w, SLOT(playClick()));
    connect(this, SIGNAL(operateRepeat(QString)), w, SLOT(repeat(QString)));
    connect(this, SIGNAL(operateStop()), w, SLOT(stop()));

    playThread.start();
}

void SoundPlayer::play(const QString &filename)
{

    emit operate(filename);
}

void SoundPlayer::emitClick()
{

    emit click();
}

void SoundPlayer::repeat(const QString &filename)
{

    emit operateRepeat(filename);
}

void SoundPlayer::stopPlay()
{
    emit operateStop();
}

void SoundPlayer::playClick()
{
    if (instance == 0)
        instance = new SoundPlayer;

    instance->emitClick();
}

void SoundPlayer::playStart()
{
    if (instance == 0)
        instance = new SoundPlayer;

    instance->play("/falinux/sounds/start.wav");
}

void SoundPlayer::playStop()
{
    if (instance == 0)
        instance = new SoundPlayer;

    instance->play("/falinux/sounds/stop.wav");
}

void SoundPlayer::playError1()
{
    if (instance == 0)
        instance = new SoundPlayer;

    instance->play("/falinux/sounds/error1.wav");
}

void SoundPlayer::playError2()
{
    if (instance == 0)
        instance = new SoundPlayer;

    instance->play("/falinux/sounds/error2.wav");
}

void SoundPlayer::repeatError1()
{
    if (instance == 0)
        instance = new SoundPlayer;

    instance->repeat("/falinux/sounds/error1.wav");
}

void SoundPlayer::repeatError2()
{
    if (instance == 0)
        instance = new SoundPlayer;

    instance->repeat("/falinux/sounds/error2.wav");
}

void SoundPlayer::stop()
{
    if (instance == 0)
        instance = new SoundPlayer;

    instance->stopPlay();
}