biyanqing01
2017-12-05 11:16 阅读 4.4k
已采纳

Qt QSoundEffect播放声音文件不成功

5

//这是头文件
#pragma once
#include
#include
#include "ui_QtGuiApplication2.h"

class QtGuiApplication2 : public QMainWindow
{
Q_OBJECT

public:
QtGuiApplication2(QWidget parent = Q_NULLPTR);
QSoundEffect
_BackMusic;
void playsound();
private:
Ui::QtGuiApplication2Class ui;
};

//这是Cpp文件
#include "QtGuiApplication2.h"

QtGuiApplication2::QtGuiApplication2(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
_BackMusic = new QSoundEffect;
playsound();
}

void QtGuiApplication2::playsound()
{
_BackMusic->setSource(QUrl::fromLocalFile("Summer.mp3"));
_BackMusic->setLoopCount(QSoundEffect::Infinite);
_BackMusic->setVolume(0.9);
_BackMusic->play();
}
编译通过,断点调试playsound函数也执行,为何声音还是播放不出来。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    qq_37465444 qq_37465444 2017-12-05 11:31

    #pragma once
    #include
    #include
    #include "ui_QtGuiApplication2.h"
    class QtGuiApplication2 : public QMainWindow
    {
    Q_OBJECT
    public:
    QtGuiApplication2(QWidget parent = Q_NULLPTR);
    QSoundEffect _BackMusic;
    void playsound();
    private:
    Ui::QtGuiApplication2Class ui;
    };
    #include "QtGuiApplication2.h"
    QtGuiApplication2::QtGuiApplication2(QWidget *parent)
    : QMainWindow(parent)
    {
    ui.setupUi(this);
    _BackMusic = new QSoundEffect;
    playsound();
    }
    void QtGuiApplication2::playsound()
    {
    _BackMusic->setSource(QUrl::fromLocalFile("Summer.mp3"));
    _BackMusic->setLoopCount(QSoundEffect::Infinite);
    _BackMusic->setVolume(0.9);
    _BackMusic->play();
    }

    点赞 评论 复制链接分享
  • smilehusky cc哈士奇 2017-12-05 13:25

    还是我来解决吧!

    The QSoundEffect class provides a way to play low latency sound effects.
    This class allows you to play uncompressed audio files (typically WAV files) in a generally lower latency way, and is suitable for "feedback" type sounds in response to user actions (e.g. virtual keyboard sounds, positive or negative feedback for popup dialogs, or game sounds). If low latency is not important, consider using the QMediaPlayer class instead, since it supports a wider variety of media formats and is less resource intensive.
    这是Qt 帮助文档中的一段话,相信你已经明白了 QSoundEffect只能播放WAV格式文件

    然后你想播放mp3 没问题 这么来
    先介绍一个类
    The QMediaPlayer class allows the playing of a media source.
    The QMediaPlayer class is a high level media playback class. It can be used to playback such content as songs, movies and internet radio. The content to playback is specified as a QMediaContent object, which can be thought of as a main or canonical URL with additional information attached. When provided with a QMediaContent playback may be able to commence.

    这个类是可以播放MP3格式的 原谅我英语不好 就不翻译了哈 我贴一下我的播放代码,

     #include "widget.h"
    #include <QApplication>
    #include <QMediaPlayer>
    #include <QUrl>
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        Widget w;
        QMediaPlayer* player = new QMediaPlayer();
        player->setMedia(QUrl::fromLocalFile("G:\\QtProject\\testSound\\bk.mp3"));
        player->setVolume(10);
        player->play();
        w.show();
    
        return a.exec();
    }
    

    注意 同样的 .pro文件中 添加 QT += multimedia

    貌似是第二次解答你的问题了 可以 加qq:836220736 本人也是小白 可共同学习(添加请注明来意)

    望采纳!!!

    点赞 3 评论 复制链接分享

相关推荐