Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt, How to use QMediaplayer::isvideoAvailable()

Qt, How to use QMediaplayer::isvideoAvailable()

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 800 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Shahroozleon
    wrote on last edited by Shahroozleon
    #1

    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
    }
    
    1 Reply Last reply
    0
    • B Offline
      B Offline
      Bonnie
      wrote on last edited by
      #2

      The file is not loaded after setMedia, so it doesn't know there is video or not.
      You need to connect to the mediaStatusChanged signal and wait until the status changes to QMediaPlayer::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.

      1 Reply Last reply
      2
      • S Offline
        S Offline
        Shahroozleon
        wrote on last edited by
        #3

        @Bonnie Could you please write the code about connecting that signal, I'm having difficulty to make it appropriately. thanks

        B 1 Reply Last reply
        0
        • S Shahroozleon

          @Bonnie Could you please write the code about connecting that signal, I'm having difficulty to make it appropriately. thanks

          B Offline
          B Offline
          Bonnie
          wrote on last edited by
          #4

          @Shahroozleon

          connect(player, &QMediaPlayer::mediaStatusChanged, this, [=](QMediaPlayer::MediaStatus status){
              if(status == QMediaPlayer::LoadedMedia) {
                  qDebug() << "isVideoAvailable: " << player->isVideoAvailable();
              }
          });
          
          1 Reply Last reply
          2
          • S Offline
            S Offline
            Shahroozleon
            wrote on last edited by
            #5

            @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.

            B 1 Reply Last reply
            0
            • S Shahroozleon

              @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.

              B Offline
              B Offline
              Bonnie
              wrote on last edited by Bonnie
              #6

              @Shahroozleon
              No, You cannot do that.
              The loading is asynchronous.
              So you won't get the result in on_openButton_clicked.
              You just call setMedia 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.

              1 Reply Last reply
              2

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved