Phonon Qt - 单击按钮时播放声音
我需要在单击按钮时播放声音,我有这个:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
文件路径“Click/sound.wav”可能没有指向您认为它指向的位置。
在调用 setCurrentSource() 函数之前尝试一下:
如果 Click 目录应该与您的 exe 位于同一目录中,请创建如下路径:
我建议使用 Qt 资源系统。然后你会像这样指向声音文件:
Probably the file path "Click/sound.wav" doesn't point where you think it points.
Try this before calling the setCurrentSource()-function:
If the Click directory is supposed to be in the same directory as your exe, create the path like this:
And I would suggest using Qt resource system. Then you would point to the sound file like this:
您至少应该将信号
stateChanged(Phonon::State, Phonon::State)
从MediaObject
对象连接到自定义插槽以检测错误:如果状态更改为Phonon::ErrorState
错误原因可以通过QMediaObject::errorString()
访问。You should at least connect the signal
stateChanged(Phonon::State, Phonon::State)
from yourMediaObject
object to a custom slot to detect errors: if the state changes toPhonon::ErrorState
the reason of the error might be accessible throughQMediaObject::errorString()
.