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. QMediaPlayer - setPosition() problem
Forum Updated to NodeBB v4.3 + New Features

QMediaPlayer - setPosition() problem

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 2.4k Views 2 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.
  • Z Offline
    Z Offline
    Zbigniew-Sch
    wrote on last edited by
    #1

    I want to set start position in "QMediaPlayer" object using setPosition() function, but unfortunately it doesn't work. Does anyone have an idea how to set the starting point correctly?
    Thank you.

    Zbigniew

    SGaistS 1 Reply Last reply
    1
    • Z Zbigniew-Sch

      In the header file:

      int m_iVideoPosition;
      

      //======================================

      mediaPlayer->setVideoOutput(vwidget);
      mediaPlayer->setAudioOutput(audioOutput);
      mediaPlayer->setSource(QUrl::fromLocalFile("myvideo.mp4"));
      audioOutput->setVolume(0.002);
      mediaPlayer->play();
      m_iVideoPosition = 200000;
      
      connect(mediaPlayer, &QMediaPlayer::positionChanged, this, &QMyMediaPlayer::positionChanged);
      
      connect(mediaPlayer,&QMediaPlayer::mediaStatusChanged,this,[=](QMediaPlayer::MediaStatus status)
      {
          if(status==QMediaPlayer::MediaStatus::BufferedMedia)
          {
              mediaPlayer->setPosition(m_iVideoPosition);
          }
      }
      

      //=======================================

      void QMyMediaPlayer::positionChanged(qint64 position)
      {
             if (m_iVideoPosition != 0 && position > m_iVideoPosition)
             {
                   m_iVideoPosition = 0;
                   pause();
             }
      }
      
      KaguroK Offline
      KaguroK Offline
      Kaguro
      wrote on last edited by
      #10

      @Zbigniew-Sch said in QMediaPlayer - setPosition() problem:

      if(status==QMediaPlayer::MediaStatus::BufferedMedia)
      {
      mediaPlayer->setPosition(m_iVideoPosition);
      }

      Works like a charm, you are my hero today! :D

      1 Reply Last reply
      1
      • Z Zbigniew-Sch

        I want to set start position in "QMediaPlayer" object using setPosition() function, but unfortunately it doesn't work. Does anyone have an idea how to set the starting point correctly?
        Thank you.

        Zbigniew

        SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi,

        When/How are you doing that ?
        Which version of Qt ?
        On which OS ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • Z Offline
          Z Offline
          Zbigniew-Sch
          wrote on last edited by
          #3

          I want to start a video (mp4) from position "x" not from "0".
          I have Qt version 6.4 on Windows 10

          B 1 Reply Last reply
          0
          • Z Zbigniew-Sch

            I want to start a video (mp4) from position "x" not from "0".
            I have Qt version 6.4 on Windows 10

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

            @Zbigniew-Sch Do you set the position before or right after you call play()?
            You need wait the player to load and buffer the media.
            connect the mediaStatusChanged signal and set the postion when the status is QMediaPlayer::BufferedMedia.

            Z 1 Reply Last reply
            1
            • B Bonnie

              @Zbigniew-Sch Do you set the position before or right after you call play()?
              You need wait the player to load and buffer the media.
              connect the mediaStatusChanged signal and set the postion when the status is QMediaPlayer::BufferedMedia.

              Z Offline
              Z Offline
              Zbigniew-Sch
              wrote on last edited by
              #5

              Thank you, it works fine.
              But I have the next problem, I want to start the video, then position it at position "x" and pause.
              Where can I call the "pause()" function and set the position "x"?
              I made several attempts, but the position (mostly is 0) is wrong or the "QVideoWidget" content is black

              B 1 Reply Last reply
              0
              • Z Zbigniew-Sch

                Thank you, it works fine.
                But I have the next problem, I want to start the video, then position it at position "x" and pause.
                Where can I call the "pause()" function and set the position "x"?
                I made several attempts, but the position (mostly is 0) is wrong or the "QVideoWidget" content is black

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

                @Zbigniew-Sch Not sure what's your case, mine works when putting pause() right before or right after setPosition().
                But it may depends on the media service plugin behaviour, for example when using windowsmediafoundation plugin under Windows, I just can't make the media pause.

                1 Reply Last reply
                0
                • KaguroK Offline
                  KaguroK Offline
                  Kaguro
                  wrote on last edited by Kaguro
                  #7

                  Hi Guys! I have a similar problem with my code:

                      QVideoWidget *vwidget = new QVideoWidget;
                      QAudioOutput *audioOutput = new QAudioOutput;
                      QMediaPlayer *mediaPlayer = new QMediaPlayer;
                  
                      mediaPlayer->setVideoOutput(vwidget );
                      mediaPlayer->setAudioOutput(audioOutput);
                      mediaPlayer->setSource(QUrl::fromLocalFile("myvideo.mp4"));
                      audioOutput->setVolume(0.002);
                      mediaPlayer->play();
                  
                      connect(mediaPlayer,&QMediaPlayer::mediaStatusChanged,this,[=](QMediaPlayer::MediaStatus status){
                          if(status==QMediaPlayer::MediaStatus::BufferedMedia)
                          {
                              mediaPlayer->pause();
                              mediaPlayer->setPosition(200000);
                              mediaPlayer->pause();
                          }
                  
                      });
                      vwidget ->show();
                  

                  its jump to the correct position of the video BUT the video didn't pause going on from the position... but why?:(

                  1 Reply Last reply
                  0
                  • Z Offline
                    Z Offline
                    Zbigniew-Sch
                    wrote on last edited by
                    #8
                    This post is deleted!
                    1 Reply Last reply
                    0
                    • Z Offline
                      Z Offline
                      Zbigniew-Sch
                      wrote on last edited by
                      #9

                      In the header file:

                      int m_iVideoPosition;
                      

                      //======================================

                      mediaPlayer->setVideoOutput(vwidget);
                      mediaPlayer->setAudioOutput(audioOutput);
                      mediaPlayer->setSource(QUrl::fromLocalFile("myvideo.mp4"));
                      audioOutput->setVolume(0.002);
                      mediaPlayer->play();
                      m_iVideoPosition = 200000;
                      
                      connect(mediaPlayer, &QMediaPlayer::positionChanged, this, &QMyMediaPlayer::positionChanged);
                      
                      connect(mediaPlayer,&QMediaPlayer::mediaStatusChanged,this,[=](QMediaPlayer::MediaStatus status)
                      {
                          if(status==QMediaPlayer::MediaStatus::BufferedMedia)
                          {
                              mediaPlayer->setPosition(m_iVideoPosition);
                          }
                      }
                      

                      //=======================================

                      void QMyMediaPlayer::positionChanged(qint64 position)
                      {
                             if (m_iVideoPosition != 0 && position > m_iVideoPosition)
                             {
                                   m_iVideoPosition = 0;
                                   pause();
                             }
                      }
                      
                      KaguroK 1 Reply Last reply
                      1
                      • Z Zbigniew-Sch

                        In the header file:

                        int m_iVideoPosition;
                        

                        //======================================

                        mediaPlayer->setVideoOutput(vwidget);
                        mediaPlayer->setAudioOutput(audioOutput);
                        mediaPlayer->setSource(QUrl::fromLocalFile("myvideo.mp4"));
                        audioOutput->setVolume(0.002);
                        mediaPlayer->play();
                        m_iVideoPosition = 200000;
                        
                        connect(mediaPlayer, &QMediaPlayer::positionChanged, this, &QMyMediaPlayer::positionChanged);
                        
                        connect(mediaPlayer,&QMediaPlayer::mediaStatusChanged,this,[=](QMediaPlayer::MediaStatus status)
                        {
                            if(status==QMediaPlayer::MediaStatus::BufferedMedia)
                            {
                                mediaPlayer->setPosition(m_iVideoPosition);
                            }
                        }
                        

                        //=======================================

                        void QMyMediaPlayer::positionChanged(qint64 position)
                        {
                               if (m_iVideoPosition != 0 && position > m_iVideoPosition)
                               {
                                     m_iVideoPosition = 0;
                                     pause();
                               }
                        }
                        
                        KaguroK Offline
                        KaguroK Offline
                        Kaguro
                        wrote on last edited by
                        #10

                        @Zbigniew-Sch said in QMediaPlayer - setPosition() problem:

                        if(status==QMediaPlayer::MediaStatus::BufferedMedia)
                        {
                        mediaPlayer->setPosition(m_iVideoPosition);
                        }

                        Works like a charm, you are my hero today! :D

                        1 Reply Last reply
                        1
                        • Z Zbigniew-Sch has marked this topic as solved on

                        • Login

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