Can't get duration video with a QMediaPlayer
-
Hi everybody !
I'm trying to get the duration of a video that I play with a QMediaPlayer but this don't work. I tried a lot of things like :
myplayer->duration(quint64); but it didn't worked.I saw that I could do that with signals but whene I try, the application say this following error message : "
signals :
void QMediaPlayer::durationChanged(qint64 duration);Here is my cpp :
//In a function 1 videowidget = new QVideoWidget(this); player = new QMediaPlayer(videowidget); player -> setVideoOutput(videowidget); videowidget->setAspectRatioMode(Qt::KeepAspectRatio); //In a function 2 player->setMedia(QUrl::fromLocalFile("/home/alexandre/Documents/qt/pt2_v3.1/video/decompte.mp4")); player->play(); connect(player, SIGNAL(durationChanged(duration)), this, SLOT(Temps(to, duration)));
Here is my class code :
class Window : public QWidget { public : qint64 duration; QMediaPlayer *player; QVideoWidget *videowidget; };
My function Temps :
void Window::Temps(int to, qint64 duration) { //Countdown std::cout <<"Durée avant réduction="<<duration<< std::endl; time = duration - 10; // On retire 10s aux temps du media pour declencher les boutons avant std::cout << "Durée après réduction=" <<time<< std::endl; //Timer duraction timer = new QTimer; timer->setSingleShot(true); timer->start(time); connect(timer, &QTimer::timeout, [this, to] { this->Attente_boutons(to); }); }
There is many other thing in my code but they don't concern my player.
Thank's !
Alexandre. -
I tried that whithout my wired signal :
void Window::Temps(int to) { std::cout<<"Passage par Temps"<<std::endl; qint64 duration=player->duration(); std::cout<<"Duration="<<duration<<std::endl; Reini_boutons(); //Compte à rebour time = duration - 10; // On retire 10s aux temps du media pour declencher les boutons avant std::cout << "Time=" <<time<< std::endl; //Declaration du timer timer = new QTimer; timer->setSingleShot(true); timer->start(time); connect(timer, &QTimer::timeout, [this, to] { this->Attente_boutons(to); }); }
time is a member qint64.
Here,
Duration=-1
Time=-11 -
I tried that whithout my wired signal :
void Window::Temps(int to) { std::cout<<"Passage par Temps"<<std::endl; qint64 duration=player->duration(); std::cout<<"Duration="<<duration<<std::endl; Reini_boutons(); //Compte à rebour time = duration - 10; // On retire 10s aux temps du media pour declencher les boutons avant std::cout << "Time=" <<time<< std::endl; //Declaration du timer timer = new QTimer; timer->setSingleShot(true); timer->start(time); connect(timer, &QTimer::timeout, [this, to] { this->Attente_boutons(to); }); }
time is a member qint64.
Here,
Duration=-1
Time=-11@saxoalex
then most probably the used codec can't determine the duration.
Have you tried with another video file to check against?