Search code examples
macosqtmultimediaqmediaplayer

Can QMediaPlayer play .wav audio files?


I wrote a test application to play '.wav' audio files using qt(latest version) on mac (OSX Yosemite Version 10.10.5). Here is the code from cpp file.

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

and here is the output I am getting

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

I don't understand why I am getting Format error and however, the same code (except the path of the track) in windows is working fine. Is there a fix for QMediaPlayer on Mac or Should I be using other APIs to play .wav files?

Any help would be appreciated! Thanks.


Solution

  • I suspect the reason of the failure (or at least one of them) is that you are passing a file path to QUrl instead of a URL. So either you set "file:///Users/testUser/Library/Metal.wav" or you use QUrl::fromLocalFile("/Users/testUser/Library/Metal.wav").