Qt, How to use QMediaplayer::isvideoAvailable()
-
Hi my question is How to use QMediaplayer::isvideoAvailable() function to determine if a media file has video availability?
I have tried to use this function like this but that doesn't work at all.
Please help me to use this function as well thanks.MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { //... connect(player, &QMediaPlayer::mediaStatusChanged, this, &MainWindow::isFileVideo); } bool MainWindow::isFileVideo(QMediaPlayer::MediaStatus input) { if(input == QMediaPlayer::LoadedMedia && player->isVideoAvailable() == true) return true; else return false; } void MainWindow::on_openButton_clicked() { QString filter { "Music File (*.mp3) ;; Audio File (*.flac) ;; Sound File (*.wav) ;; Clip File (*.mp4) ;; Video File (*.mkv) ;; " "Digital Video (*.mpg) ;; Windows Media Video (*.wmv)" }; QString filePath { QFileDialog::getOpenFileName(this, "Open a music file", "C://", filter) }; QMediaContent file(QUrl::fromLocalFile(filePath)); QFileInfo fileInfo{ filePath }; player->setMedia(file); if(isFileVideo(player->mediaStatus()) == true) // here any proper condition which makes this work correctly { //showing video widget here } //showing added songs here }
-
The file is not loaded after
setMedia
, so it doesn't know there is video or not.
You need to connect to themediaStatusChanged
signal and wait until the status changes toQMediaPlayer::LoadedMedia
.You can also read that from the doc:
void QMediaPlayer::setMedia(const QMediaContent &media, QIODevice *stream = nullptr)
...
Note: This function returns immediately after recording the specified source of the media. It does not wait for the media to finish loading and does not check for errors. Listen for the mediaStatusChanged() and error() signals to be notified when the media is loaded and when an error occurs during loading. -
@Bonnie Could you please write the code about connecting that signal, I'm having difficulty to make it appropriately. thanks
-
connect(player, &QMediaPlayer::mediaStatusChanged, this, [=](QMediaPlayer::MediaStatus status){ if(status == QMediaPlayer::LoadedMedia) { qDebug() << "isVideoAvailable: " << player->isVideoAvailable(); } });
-
@Bonnie thanks so much and I have to admit that I'm such a noob haven't seen this kind of connect. But I dont know where to use this, cause i get the true massage after i close my program.
Now let me explain more clear what am I doing, I have function which opens file and load it and in there i want to recognize that this loaded file is video or music and I cant get the result I want perhaps you could be more patient with me and check this out too, thank you so much; I updated the code. -
@Shahroozleon
No, You cannot do that.
The loading is asynchronous.
So you won't get the result inon_openButton_clicked
.
You just callsetMedia
there and handle the rest in the connected function, which is also meaningless to have a return value.
Before starting Qt programming, it is necessary to get familiar with the signals and slots mechanism which is very basic in Qt.
You may need to read the documentation more to understand how to act to a signal.