LittleMerryn LittleMerryn - 1 month ago 13
C++ Question

How do I know the format of audio file?

I want to make a music player and I want to filter out files not audio file on opening.

Should I use QAudioDecoder?

But every file I checked with QAudioDecoder tells me the same codec "audio/pcm".

QAudioDecoder decoder;
decoder.setSourceFilename(fileUrl.toLocalFile());
qDebug() << decoder.audioFormat().codec();


How do I know if the audio file's format is supported by Qt media player by not just checking if the file's name ends with ".mp3" or ".wav"?

And I found that some files' duration incorrect when playing on my music player.
But when I play the file with other music player apps, it gives correct duration on other music players.

Or my app can't play some files but others can.

the console says :


DirectShowPlayerService::doSetUrlSource: Unresolved error code
0x80070002 (?t?Χ??????w?????C)


but after I change the file name, it can be opened.

How should I fix those on my music player instead of fixing the files' probably wrong or unsupported formats one by one?




I wrote this code to check if the file I opened is a supported audio file.

QMediaPlayer audioChecker;
audioChecker.setMedia(fileUrl);
qDebug() << audioChecker.media().canonicalUrl().fileName();
qDebug() << audioChecker.isAudioAvailable();
if(audioChecker.error() == QMediaPlayer::NoError) {
qDebug() << "no error";
}
if(audioChecker.error() == QMediaPlayer::FormatError) {
qDebug() << "format error";
}


and it tells me no error no matter what file I tested.

console:


"musicplayer.exe"

false

no error

"musicplayer.exe"

false

no error

"musicplayer.ilk"

false

no error

"musicplayer.exe"

false

no error

"a song.mp3"

false

no error


But how can files not audio file get no error?
Why do audio file's audio not available?




Never mind. I just read that setMedia() return immediately and doesn't wait for the media to be loaded. so it's normal it gets no audio and no error because it's not loaded.
I guess I'll make a thread class to check my file.

Answer Source

From Qt's documentation:

For playing media or audio files that are not simple, uncompressed audio, you can use the QMediaPlayer C++ class […] The compressed audio formats supported does depend on the operating system environment, and also what media plugins the user may have installed.

A simple example of how to use the QMediaPlayer class:

player = new QMediaPlayer;
// ...
player->setMedia(QUrl::fromLocalFile("/Users/me/Music/coolsong.mp3"));
player->setVolume(50);
player->play();

To verify that the codec is supported, you can use the error() method of the QMediaPlayer class which will return you an error code (see the documentation).

So, after calling setMedia(), you can do something like:

if (player->error() == QMediaPlayer::FormatError)
{
    // Codec/format unsupported
}

About the error code you get when opening files:

  • check that you have no \ characters in your path (if you have some, escape them with another \)
  • check if you have spaces, special characters, etc.