我写了一个由两部分组成的轻音频播放器
核心:它有一个公共接口和一个私有的接口。在pimpl(AudioPlayerPrivate)中,我正在创建音频后端(解码,混合)并将其移动到工作线程。
AudioPlayerPrivate::AudioPlayerPrivate(QObject *parent) :
QObject(parent)
{
playerThread_ = new QThread(this);
playerCore_ = new AudioPlayerCore;
qDebug()<<"AudioPlayerCore thread:"<<playerCore_->thread();
playerCore_->moveToThread(playerThread_);
qDebug()<<"AudioPlayerCore thread after moving to playerThread:"<<playerCore_->thread();
playerThread_->start();
}
gui:它正在从动态库中加载核心。
现在让我想知道的是,当gui加载并且我正在播放音乐时,当我点击(并且不释放)鼠标在窗口装饰的关闭按钮(X)上时,音频暂停直到我释放鼠标。当我使用不同的线程进行播放时,这怎么可能?
调试输出如下:
PlayerGUI thread: 0x4a83a8
AudioPlayer thread: 0x4a83a8
AudioPlayerPrivate Constructor
AudioPlayerPrivate thread (this->thread()): QThread(0x4a83a8)
AudioPlayerPrivate thread (QCoreApplication::instance()->thread()): QThread(0x4a83a8)
AudioPlayerPrivate thread (QThread::currentThread): QThread(0x4a83a8)
AudioPlayerCore Constructor
AudioPlayerCore thread: QThread(0x4a83a8)
AudioPlayerCore thread after moving to playerThread: QThread(0x4bb808)
AudioPlayerPrivate::play()
AudioPlayerCore::onPlayRequest()
AudioPlayerCore thread (this->thread()): QThread(0x4bb808)
AudioPlayerCore thread (QCoreApplication::instance()->thread()): QThread(0x4a83a8)
AudioPlayerCore thread (QThread::currentThread): QThread(0x4a83a8)
从输出看起来我还没有成功地将核心移动到线程中。 3个线程调试中只有一个(上面最后3行)实际显示了新线程的ID。 我缺少的线程是否有明显的东西?
以下是我如何调用播放开始(在AudioPlayerPrivate中):
connect(this,SIGNAL(playRequest(InputSource*,bool)),
playerCore_,SLOT(onPlayRequest(InputSource*,bool)),
Qt::DirectConnection);
bool startPlaybackImmediately = false;
emit playRequest(source,startPlaybackImmediately);