QMediaPlayer 可以播放.wav 音频文件吗?

Can QMediaPlayer play .wav audio files?

我在 mac(OSX Yosemite 版本 10.10.5)上使用 qt(最新版本)编写了一个测试应用程序来播放“.wav”音频文件。这是来自 cpp 文件的代码。

#include "widget.h"
Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
m_player = new QMediaPlayer(this);
connect(m_player, SIGNAL(mediaStatusChanged(QMediaPlayer::MediaStatus)), this, SLOT(onMediaStatusChanged()));
connect(m_player, SIGNAL(positionChanged(qint64)), this, SLOT(onProgressed(qint64)));
m_player->setMedia(QMediaContent(QUrl("/Users/testUser/Library/Metal.wav")));
m_player->play();
}

Widget::~Widget()
{
}

void Widget::onProgressed(qint64 pos)
{
    qDebug()<<"Position is:"<<pos;
}

void Widget::onMediaStatusChanged()
{
    qDebug()<<"Media Status Changed:" << m_player->mediaStatus() << " " << m_player->error();
}

这是我得到的输出

Media Status Changed: QMediaPlayer::LoadingMedia QMediaPlayer::NoError
[11:16:13.300] FigByteFlumeCustomURLOpen signalled err=-12936 (kFigByteFlumeError_BadState) (no provider) at /SourceCache/CoreMedia/CoreMedia-1562.240/Prototypes/FigHTTP/FigByteFlumeCustomURL.c line 1486
Media Status Changed: QMediaPlayer::InvalidMedia QMediaPlayer::FormatError

我不明白为什么会出现格式错误,但是 windows 中的相同代码(除了轨道路径)工作正常。 Mac 上的 QMediaPlayer 是否有修复,或者我应该使用其他 API 来播放 .wav 文件吗?

如有任何帮助,我们将不胜感激!谢谢

我怀疑失败的原因(或至少其中之一)是您将文件路径传递给 QUrl 而不是 URL。所以要么你设置 "file:///Users/testUser/Library/Metal.wav" 要么你使用 QUrl::fromLocalFile("/Users/testUser/Library/Metal.wav").