通过QAudioOutput播放wav文件时只发出嗡嗡声

时间:2016-08-12 10:21:11

标签: c++ windows qt audio

在Windows 7桌面和笔记本电脑上使用Qt 5.5.1。

我只能听到嗡嗡的声音,没有别的。请指导。

链接到wav文件,我试过 - https://www.dropbox.com/s/frfy43d8hznptgf/c.wav?dl=0

我也尝试过使用mp3文件。我得到的只是嗡嗡声。

可重复的示例:

#include <QCoreApplication>
#include <QAudioOutput>
#include <QFile>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QAudioOutput* audioOutpu;

    QFile sourceFile;
    sourceFile.setFileName("c.wav");
    bool p = sourceFile.open(QIODevice::ReadOnly);
    if (p == false)
        qDebug() << "no file";
    else
        qDebug() << "yes file";


    QAudioDeviceInfo d1;
    QList<QAudioDeviceInfo> l1 = d1.availableDevices(QAudio::AudioOutput);

    qDebug() << "======================================================";
    qDebug() << l1.first().supportedCodecs();
    qDebug() << l1.first().supportedChannelCounts();
    qDebug() << l1.first().supportedSampleTypes();
    qDebug() << l1.first().supportedSampleRates();
    qDebug() << l1.first().supportedSampleSizes();

    QAudioFormat desiredFormat1;
    desiredFormat1.setChannelCount(2);
    desiredFormat1.setByteOrder(QAudioFormat::LittleEndian);
    desiredFormat1.setCodec("audio/pcm");
    desiredFormat1.setSampleType(QAudioFormat::SignedInt);
    desiredFormat1.setSampleRate(44100);
    desiredFormat1.setSampleSize(16);

    QAudioDeviceInfo info1(QAudioDeviceInfo::defaultOutputDevice());
    if (!info1.isFormatSupported(desiredFormat1))
    {
           qWarning() << "Default format not supported, trying to use the nearest.";
           desiredFormat1 = info1.preferredFormat();
    }

    audioOutpu = new QAudioOutput(desiredFormat1);
    audioOutpu->setVolume(1.0);

    audioOutpu->start(&sourceFile);
    qDebug() << "bbbbbbbbbb";
    QEventLoop loop;
    QObject::connect(audioOutpu, SIGNAL(stateChanged(QAudio::State)), &loop, SLOT(quit()));
    do {
        loop.exec();
    } while(audioOutpu->state() == QAudio::ActiveState);

    return a.exec();
}

输出:

enter image description here

1 个答案:

答案 0 :(得分:4)

您的wav文件实际上不是WAVE format,这里是与文件关联的MediaInfo输出:

Format                                   : MPEG Audio
File size                                : 4.67 MiB
Duration                                 : 5mn 6s
Overall bit rate mode                    : Constant
Overall bit rate                         : 128 Kbps
Track name                               : Careless  Whisper
Performer                                : Kenny G
Genre                                    : Instrumental

Audio
Format                                   : MPEG Audio
Format version                           : Version 1
Format profile                           : Layer 3
Mode                                     : Joint stereo
Mode extension                           : MS Stereo
Duration                                 : 5mn 6s
Bit rate mode                            : Constant
Bit rate                                 : 128 Kbps
Channel(s)                               : 2 channels
Sampling rate                            : 44.1 KHz
Compression mode                         : Lossy
Stream size                              : 4.67 MiB (100%)

它是MPEG Audio压缩文件,这就是为什么你不能使用"audio/pcm"编解码器来播放它,我将你的音频文件解压缩到PCM中,你的代码能够正确播放。以下是MediaInfo用我的文件输出的内容:

Format                                   : Wave
File size                                : 51.6 MiB
Duration                                 : 5mn 6s
Overall bit rate mode                    : Constant
Overall bit rate                         : 1 411 Kbps

Audio
Format                                   : PCM
Format settings, Endianness              : Little
Format settings, Sign                    : Signed
Codec ID                                 : 1
Duration                                 : 5mn 6s
Bit rate mode                            : Constant
Bit rate                                 : 1 411.2 Kbps
Channel(s)                               : 2 channels
Sampling rate                            : 44.1 KHz
Bit depth                                : 16 bits
Stream size                              : 51.6 MiB (100%)

如果您需要播放压缩音频格式,请考虑使用QMediaPlayer,看看here