Question

I want to find the length of a song in seconds so that I can make a playlist with the M3U format, but I don't know how to get that info from the file. I am using id3lib and also Qt.

I've tried:

ID3_Frame* myFrame = myTag.Find(ID3FID_SONGLEN);
ID3_Frame* myFrame = myTag.Find(ID3FID_TIME);
ID3_Frame* myFrame = myTag.Find(ID3FID_SIZE);

but myFrame was NULL. I tried other parameters and worked, like ID3FID_ALBUM. I really don't know what to try. I didn't find anything in the id3lib API.

I know that Qt can give me that info, but can't I use id3lib?

Later edit: I've read that MediaObject::totalTime() gives me the time only if the song is played but I want that info, regarding that the songs are played or not; I need this info to write it in a file.

LATER EDIT:

I am trying to use Phonon from Qt for the length, but i receive a lot of errors.

#include <Phonon/MediaSource>
#include <Phonon/MediaObject>
#include <iostream>
#include <Qstring>

/* stuff */

Phonon::MediaObject *mediaObject = new Phonon::MediaObject();
QString fileName("D:/melodie.mp3");
mediaObject->setCurrentSource(fileName);
cout << mediaObject->totalTime() << endl;

I get a lot of unresolved external symbol

main.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall Phonon::MediaObject::MediaObject(class QObject *)" (_imp??0MediaObject@Phonon@@QAE@PAVQObject@@@Z) referenced in function _main

I am using QCreator, my application is simple, just a window that says hello(it works if i don't use Phonon); i just can't make it run; i've allocated static mediaObject, i've made a MediaSource and tried to pass it, but it doesn't work(here it says that can't covert from MediaSource* to MediaSource &). What could be the problem?

Was it helpful?

Solution

You can use Phonon::MediaObject::totalTime() to get the length of the song in milliseconds.

id3lib does not provide a function to directly find the length of a song because track length is not part of the ID3v1 or ID3v2 "standards". They are merely for tagging. If you're getting a 0 when you try the code in your question, it probably means that those tags haven't been set yet. As noted in the docs, "...the Find() method will return a NULL pointer if no such frame can be found."

OTHER TIPS

Use MediaObject::totalTime after setting correctly the media source

I suggest you to use totalTimeChanged(qint64 newTotalTime) signal. It is more careful way to capture length of the track, because you don't need to manually check state of mediaObject.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top