.exe文件关闭时,我点击按钮在用户界面,应该播放音乐文件

.exe file closes when i click the button in the userinterface which is supposed to play music file

本文关键字:文件 用户界面 音乐 播放 按钮 exe      更新时间:2023-10-16

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

#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);
}

我有三个建议:

首先,添加错误检查。

第二,考虑使用Ogg Vorbis,如果你有mp3的问题。

第三,将指针移动为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支持的常见问题解答

在faq页面中也有一个警告:

从1.9.2版本开始,Audiere支持MP3图书馆。然而,很少有兼容lgpl的MP3代码它适用于各种mp3和硬件。我强烈推荐使用Ogg Vorbis来满足你所有的音乐需求。它使用听起来更好。

当Audiere清理时

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

当你完成使用Audiere,只是让RefPtr对象出去作用域,它们会自动清理自己。如果你真的必须在对象的指针超出作用域之前删除对象,只需将指针设置为0。

希望对你有帮助。