gpt4 book ai didi

c++ - 当我单击用户界面中应该播放音乐文件的按钮时,.exe 文件关闭

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:57:48 24 4
gpt4 key购买 nike

我是 QT 的新手,我想通过 QT 播放音乐文件,界面包含一个播放按钮,这样当我点击播放按钮时,歌曲就会播放。现在当我运行程序时,我得到了我的界面,但不幸的是,当我单击播放按钮时,它说 .exe 文件停止工作,并且它被关闭,退出错误代码 255 getiing 显示在 QT 创建者窗口中。这是主 window.cpp 文件

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "audiere.h"
using namespace audiere;

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

//connect(ui->Number1,SIGNAL(textChanged(QString)),this,SLOT(numberChanged()));
connect(ui->play,SIGNAL(clicked()),this,SLOT(PLAY()));

}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::PLAY() {
AudioDevicePtr device(OpenDevice());
OutputStreamPtr sound(OpenSound(device,"lk.mp3",true));

sound->play();
sound->setRepeat(true);
sound->setVolume(2.0);

}

最佳答案

我有三个建议:

首先,添加错误检查。

其次,如果您在使用 mp3 时遇到问题,请考虑使用 Ogg Vorbis。

第三,将指针移动到 MainWindow 的成员变量而不是局部范围变量。 Audiere 可能过早地清理了它们。

在 Audiere 中使用错误检查

这是来自 Audiere 下载的 doc 文件夹中的“tutorial.txt”:

您需要先打开一个 AudioDevice 才能播放声音...

AudioDevicePtr device(OpenDevice());
if (!device) {
// failure
}

现在我们有了一个设备,我们实际上可以打开并播放声音了。

/*
* If OpenSound is called with the last parameter = false, then
* Audiere tries to load the sound into memory. If it can't do
* that, it will just stream it.
*/
OutputStreamPtr sound(OpenSound(device, "effect.wav", false));
if (!sound) {
// failure
}

/*
* Since this file is background music, we don't need to load the
* whole thing into memory.
*/
OutputStreamPtr stream(OpenSound(device, "music.ogg", true));
if (!stream) {
// failure
}

太好了,我们打开了一些流!我们用它们做什么?

关于最近 MP3 支持的常见问题信息

常见问题页面中还有一个警告:

As of the 1.9.2 release, Audiere supports MP3 thanks to the splay library. However, there is very little LGPL-compatible MP3 code out there that works across a wide range of MP3s and hardware. I highly recommend using Ogg Vorbis for all of your music needs. It uses less CPU time by about a factor of five and sounds better.

当 Audiere 清理时

在教程的底部,它提到了清理发生的时间:

When you are done using Audiere, just let the RefPtr objects go out of scope, and they will automatically clean themselves up. If you really must delete an object before its pointer goes out of scope, just set the pointer to 0.

希望对您有所帮助。

关于c++ - 当我单击用户界面中应该播放音乐文件的按钮时,.exe 文件关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15590197/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com