Phonon Qt - 单击按钮时播放声音

发布于 2025-01-06 20:08:09 字数 480 浏览 4 评论 0原文

我需要在单击按钮时播放声音,我有这个:

Phonon::MediaObject *clickObject = new Phonon::MediaObject(this);
clickObject->setCurrentSource(Phonon::MediaSource("Click/sound.wav");
Phonon::AudioOutput *clickOutput = new Phonon::AudioOutput(Phonon::MusicCategory, this);
Phonon::createPath(clickObject, clickOutput);

但是

void MainWindow::on_pushButton_clicked()
{
   clickObject->play();
}

没有播放声音? 我哪里错了? 谢谢。 编辑:现在可以了,这是错误的道路。

I need to play a sound when a button is clicked, I have this:

Phonon::MediaObject *clickObject = new Phonon::MediaObject(this);
clickObject->setCurrentSource(Phonon::MediaSource("Click/sound.wav");
Phonon::AudioOutput *clickOutput = new Phonon::AudioOutput(Phonon::MusicCategory, this);
Phonon::createPath(clickObject, clickOutput);

and

void MainWindow::on_pushButton_clicked()
{
   clickObject->play();
}

but no sound is played?
Where am I wrong?
Thanks.
EDIT: It works now, it was the wrong path.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苦笑流年记忆 2025-01-13 20:08:09

文件路径“Click/sound.wav”可能没有指向您认为它指向的位置。

在调用 setCurrentSource() 函数之前尝试一下:

bool exists = QFile::exists("Click/sound.wav");

如果 Click 目录应该与您的 exe 位于同一目录中,请创建如下路径:

QString filePath = QCoreApplication::applicationDirPath() + "/Click/sound.wav";
clickObject->setCurrentSource(Phonon::MediaSource(filePath));

我建议使用 Qt 资源系统。然后你会像这样指向声音文件:

clickObject->setCurrentSource(Phonon::MediaSource(":/Click/sound.wav"));

Probably the file path "Click/sound.wav" doesn't point where you think it points.

Try this before calling the setCurrentSource()-function:

bool exists = QFile::exists("Click/sound.wav");

If the Click directory is supposed to be in the same directory as your exe, create the path like this:

QString filePath = QCoreApplication::applicationDirPath() + "/Click/sound.wav";
clickObject->setCurrentSource(Phonon::MediaSource(filePath));

And I would suggest using Qt resource system. Then you would point to the sound file like this:

clickObject->setCurrentSource(Phonon::MediaSource(":/Click/sound.wav"));
此岸叶落 2025-01-13 20:08:09

您至少应该将信号 stateChanged(Phonon::State, Phonon::State)MediaObject 对象连接到自定义插槽以检测错误:如果状态更改为Phonon::ErrorState 错误原因可以通过 QMediaObject::errorString() 访问。

You should at least connect the signal stateChanged(Phonon::State, Phonon::State) from your MediaObject object to a custom slot to detect errors: if the state changes to Phonon::ErrorState the reason of the error might be accessible through QMediaObject::errorString().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文