Using Qt Mobility to get metadata from media files
Article Metadata
Tested with
Devices(s): Tested on: Nokia 5800 XpressMusic
Compatibility
Platform(s): Qt 4.x
Article
Keywords: QMediaPlayer, QMediaObject
Created: User:Kbwiki
(29 Jun 2010)
Last edited: hamishwillee
(20 Nov 2012)
Description
With the QMediaObject::availableMetaData() and QMediaObject::metaData() functions we can retrieve the metadata in a media file (audio/video). The metadata information can be only retrieved while the file is playing.
Qt Mobility APIs are included in the Qt SDK.
Solution
Define the QtMobility/Multimedia configuration in the .pro file:
CONFIG += mobility
MOBILITY = multimedia
Header files:
#include <QMediaPlayer>
#include <qtmedianamespace.h>
Implementation:
// Use QMediaPlayer as a class member
QMediaPlayer *player;
// create the player and start playback
player = new QMediaPlayer(this);
player->setMedia(QUrl::fromLocalFile("C:\\Data\\sample.mp3"));
player->setVolume( 25 );
player->play();
A method to get the metadata of the media file:
void MediaMetaDataExample::GetMetaData()
{
// Get the list of keys there is metadata available for
QList<QtMediaServices::MetaData> metadatalist = player->availableMetaData();
// Get the size of the list
int list_size = metadatalist.size();
// Define variables to store metadata key and value
QtMediaServices::MetaData metadata_key;
QVariant var_data;
for (int indx = 0; indx < list_size; indx++) {
// Get the key from the list
metadata_key = metadatalist.at(indx);
// Get the value for the key
var_data = player->metaData(metadata_key);
switch(metadata_key) {
case QtMediaServices::Title:
// Retrieve title (using var_data.toString())
break;
case QtMediaServices::SubTitle:
// Retrieve subtitle
break;
case QtMediaServices::Author:
// Retrieve author
break;
// ... see QtMediaServices for rest of the values
default:
break;
}
}
}
A method to get the extended metadata of the media file:
void MediaMetaDataExample::GetExtendedMetaData()
{
// Retrieve extended metadata key list
QStringList extn_metadata = player->availableExtendedMetaData();
int count_extn = extn_metadata.count();
QString extn_metadata_key;
QVariant extn_metadata_value;
for( int indx = 0; indx < count_extn; indx++ )
{
extn_metadata_key = extn_metadata.at(indx);
extn_metadata_value = player->extendedMetaData(extn_metadata_key);
// Process extended metadata here
// Need to convert QVariant into proper type
}
}


Swarit - In Qt 5.0 how to get metadata info of a song in Qt 5.0
void MediaMetaDataExample::GetMetaData()
{ // Get the list of keys there is metadata available for QList<QtMediaServices::MetaData> metadatalist = player->availableMetaData(); // Get the size of the list int list_size = metadatalist.size(); // Define variables to store metadata key and value QtMediaServices::MetaData metadata_key; QVariant var_data; for (int indx = 0; indx < list_size; indx++) { // Get the key from the list metadata_key = metadatalist.at(indx); // Get the value for the key var_data = player->metaData(metadata_key); switch(metadata_key) { case QtMediaServices::Title: // Retrieve title (using var_data.toString()) break; case QtMediaServices::SubTitle: // Retrieve subtitle break; case QtMediaServices::Author: // Retrieve author break; // ... see QtMediaServices for rest of the values default: break; } }}swarit 19:21, 18 November 2012 (EET)
Hamishwillee - No idea
Nokia devices use Qt 4.x so I don't think there will many people who will know the answer to this. I've updated the Qt compatibility to Qt 4.x above. You might want to check out the new module QMultimediahamishwillee 06:05, 20 November 2012 (EET)