I have been working on an application where I can traverse the system drives using QDirIterator and look for audio/video files, grab the details using QStandardItemModel and display it on QTreeview. I have been successful in displaying file name, type, size, date-modified but DURATION is something which I am not able to do.
Here is the code:
// Displays Files in Detail View on Clicking Drive
void DetailView::on_DriveView_clicked(const QModelIndex &index)
{
int m_count_row = 0;
QStandardItemModel *model = new QStandardItemModel(0,0);
QString sPath = pSystemModel->fileInfo(index).absoluteFilePath();
pSystemTreeViewModel->setRootPath(sPath);
ui->DriveListView->setRootIndex(pSystemTreeViewModel->index(sPath));
pSystemModel->setRootPath(QDir::currentPath());
pSystemModel->setFilter(QDir::NoDotAndDotDot | QDir::AllDirs );
pSystemTreeViewModel->setFilter( QDir::Files | QDir::NoDotAndDotDot );
QStringList m_list;
QDirIterator dirIt(sPath,QDirIterator::Subdirectories);
while (dirIt.hasNext())
{
dirIt.next();
if (QFileInfo(dirIt.filePath()).isFile())
{
if (QFileInfo(dirIt.filePath()).suffix() == "mp3" ||(QFileInfo(dirIt.filePath()).suffix() == "mts" ) ||(QFileInfo(dirIt.filePath()).suffix() == "m2ts" ))
{
m_list << dirIt.filePath();
QModelIndex m_index = model->index(m_count_row, 0, QModelIndex());
model->setHeaderData( 0, Qt::Horizontal, "Name" );
model->setHeaderData( 1, Qt::Horizontal, "Type" );
model->setHeaderData( 2, Qt::Horizontal, "Size" );
model->setHeaderData( 3, Qt::Horizontal, "Date Modified" );
model->setData( m_index, dirIt.fileName(), Qt::DecorationRole );
QStandardItem *itemName = new QStandardItem(dirIt.fileName());
model->setItem(m_count_row, 0, itemName);
model->setData( m_index, dirIt.fileInfo().suffix(), Qt::DecorationRole );
QStandardItem *itemExtention = new QStandardItem( dirIt.fileInfo().suffix());
model->setItem(m_count_row, 1, itemExtention);
model->setData( m_index, dirIt.fileInfo().size(), Qt::DecorationRole );
float fFileSize = dirIt.fileInfo().size();
float fFileKB = fFileSize / 1024; //kilobyte
float fFileMB = fFileKB / 1024; //megabyte
float fFinalSize = ceilf(fFileMB * 100) / 100;
QString sSizeValue = QString::number(fFinalSize);
QStandardItem *itemSize = new QStandardItem(sSizeValue + " MB");
model->setItem(m_count_row, 2, itemSize);
model->setData( m_index, dirIt.fileInfo().lastModified(), Qt::DecorationRole );
QDateTime m_time = dirIt.fileInfo().lastModified();
QString sTime = m_time.toString("dd/MM/yyyy hh:mm:ss");
QStandardItem *itemDate = new QStandardItem(sTime);
model->setItem(m_count_row, 3, itemDate);
ui->DriveListView->setModel(model);
ui->DriveListView->setRootIsDecorated(false);
m_count_row++;
}
}
pSystemTreeViewModel->setNameFilterDisables(false);
}
}
I am not sure whether I can get the duration using the above way since I couldn't find any such property. Is their any other way of doing it? I don want to use QMediaPlayer. Any other alternate solution which can help me update my above code with Duration???
Using Qt
You can try exploring the Qt's Phonon Module.
I think this function in the Phonon's MediaObject class might be helpful in getting what you are trying to achieve.
P.S. Do read the warning written at the end of that function, to avoid getting wrong results.
Edit 1: Without using Qt
Read about TagLib Library here. Check this function they have mentioned regarding getting the length of file. And it is under LGPL as well.