Stop Playing a Video from QMediaPlayer at Position X

前端 未结 1 1258
傲寒
傲寒 2021-01-14 13:37

I am new to Qt and I am using QMediaPlayer in one of my GUI projects and I want to stop the loaded video at a certain position X (input from user on a Line Edit) how would I

1条回答
  •  终归单人心
    2021-01-14 14:22

    One lesser option would be to use position() which returns the current position as a qint64 - if you call the play() method for your QMediaPlayer then use something like

      while (player.position() < input) {}
      player.stop();     // Or player.pause();
    

    it will wait until the input position is reached. But the drawback to that approach is the blocking while loop and without knowing the intended application I don't know if that would be appropriate. It is probably better to use the QMediaPlayer::positionChanged signal (which is emitted based on the QMediaPlayer's notifyInterval), something like

      connect(player, SIGNAL(positionChanged(qint64)), this, SLOT(checkPosition());
    

    where it is assumed this is the receiver and both player and input are scoped such that they are available to the slot checkPosition(). checkPosition() then looks something like

      checkPosition() {
          if (player.position() > input()) {
              player.stop();     // Or player.pause();
          }
      }
    

    Of course you can also pass the player and the input to the checkPosition() slot but I neglected that for simplicity. Hope this helps.

    0 讨论(0)
提交回复
热议问题