简体   繁体   English

通过QAudioOutput播放WAV文件时仅发出嗡嗡声

[英]Only buzzing sound while playing wav file through QAudioOutput

Using Qt 5.5.1 on a Windows 7 desktop and a laptop. 在Windows 7台式机和笔记本电脑上使用Qt 5.5.1。

I can only hear the buzzing sound, nothing else. 我只能听到嗡嗡声,没有别的。 Please guide. 请指导。

Link to the wav file, I tried - https://www.dropbox.com/s/frfy43d8hznptgf/c.wav?dl=0 我尝试链接到wav文件-https://www.dropbox.com/s/frfy43d8hznptgf/c.wav?dl =0

I have tried this with mp3 files also. 我也尝试了mp3文件。 All I get is buzzing sound. 我得到的只是嗡嗡声。

The reproducable example: 可重现的示例:

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

Output: 输出:

在此处输入图片说明

Your wav file is not really WAVE format , here is MediaInfo output associated with the file: 您的wav文件不是真正的WAVE格式 ,以下是与该文件关联的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%)

it is MPEG Audio compressed file and that's why you can't use "audio/pcm" codec to play it, I uncompressed your audio file into PCM, and your code was able to play it right. 它是MPEG音频压缩文件,这就是为什么你不能使用"audio/pcm"编解码器进行播放,我未压缩音频文件转换成PCM,和你的代码是能够发挥它的权利。 Here is what MediaInfo outputs with my file: 这是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%)

If you need to play compressed audio formats, consider using QMediaPlayer , have a look here . 如果您需要播放压缩的音频格式,请考虑使用QMediaPlayer ,请在此处查看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM