Using Qt Mobility to get metadata from media files

From Qt Wiki
Jump to navigation Jump to search

Template:ArticleMetaData

Description[edit | edit source]

With the 76ytuiytuityutyutututyutyutyu and 76ytuiytuityutyutututyutyutyu 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[edit | edit source]

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
     }
 }