0
点赞
收藏
分享

微信扫一扫

Qt,Linux: 播放声音(aplay)


Linux下,Qt开发,使用的电脑情况比较复杂,开发机是Intel cpu, 常用的验证机是飞腾(arm)cpu, 客户的目标机也是飞腾(arm)cpu, 但验证机和目标机上情况还不太一样。

 

需要用到播放声音的功能,开发机,验证机,目标机,三个电脑上来回折腾,也没找到一个统一的方法,试过QMediaPlayer,QSound,结果是开发机和验证机都搞定了,但到目标机上怎么也搞不定,遇到过的错误大致如下:

1. defaultServiceProvider::requestService(): no service found for - “org.qt-pr

2. using null output device, none available

在目标机上折腾了很长时间,在截止日前两天仍然搞不定。

于是换个思路,既然直接双击音频可以播放,说明OS层面对音频的支持没问题。可以考虑Qt以外的播放功能。

找到了aplay, 先尝试在终端执行 :

aplay hint.wav

成功,声音可以播放。

Qt,Linux: 播放声音(aplay)_ide

 然后放进代码中:

QString audioPath = "/audio/hint.wav";

QProcess p;
p.start("aplay " + audioPath);
p.waitForFinished();

测试成功,可以播放。

直接用QProcess播放的话主线程会卡一下。更进一步考虑多线程:

//ThreadPlaySound.h ********************
class ThreadPlaySound: public QThread
{
public:
ThreadPlaySound();
QString audioPath;

protected:
void run() override;
};

//ThreadPlaySound.cpp ********************
ThreadPlaySound::ThreadPlaySound()
{
}

void ThreadPlaySound::run()
{
QProcess p;
p.start("aplay " + audioPath);
p.waitForFinished();
this->exit();
}

//调用
ThreadPlaySound* t = new ThreadPlaySound();
t->audioPath = audioPath;
t->start();

测试成功,连续播放也不会卡主主线程。

aplay参考:​​aplay command in Linux with examples - GeeksforGeeks​​

举报

相关推荐

0 条评论