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. Help to understand simple thing plz _))

Help to understand simple thing plz _))

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 3 Posters 678 Views
  • 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.
  • H.323H Offline
    H.323H Offline
    H.323
    wrote on last edited by
    #1

    // Hey guys, that's my first article so sorry, I am newbie with Qt framework but have a lot of experience with C programming. //Just have read couple books about QT and got into my first trouble. The point is about misunderstanding of basics. This //example I found on official site and it works.
    player = new QMediaPlayer;
    player->setMedia(QUrl::fromLocalFile("/Users/me/Music/coolsong.mp3")); // With my own path
    player->setVolume(50);
    player->play();
    // But when i try to go like QMediaPlayer player instead of QMediaPlayer* player = new QMediaPlayer and then
    player.setMedia(QUrl::fromLocalFile("/Users/me/Music/coolsong.mp3")); // With my own path
    player.setVolume(50);
    player.play();
    // I got an error in application output tab like GStream can't pause - file://filepath.
    // Tried to rollback to normal state and then back again to error but this time can't see error, but song does not work;
    // So the question is what should I know about this thing. _))

    _))

    JonBJ 1 Reply Last reply
    0
    • H.323H H.323

      // Hey guys, that's my first article so sorry, I am newbie with Qt framework but have a lot of experience with C programming. //Just have read couple books about QT and got into my first trouble. The point is about misunderstanding of basics. This //example I found on official site and it works.
      player = new QMediaPlayer;
      player->setMedia(QUrl::fromLocalFile("/Users/me/Music/coolsong.mp3")); // With my own path
      player->setVolume(50);
      player->play();
      // But when i try to go like QMediaPlayer player instead of QMediaPlayer* player = new QMediaPlayer and then
      player.setMedia(QUrl::fromLocalFile("/Users/me/Music/coolsong.mp3")); // With my own path
      player.setVolume(50);
      player.play();
      // I got an error in application output tab like GStream can't pause - file://filepath.
      // Tried to rollback to normal state and then back again to error but this time can't see error, but song does not work;
      // So the question is what should I know about this thing. _))

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @H-323
      If you write

      QMediaPlayer player;
      

      the instance only lives as long as the scope it is declared in, and will be destroyed when it goes out of scope. Is that the cause of your error message?

      H.323H 1 Reply Last reply
      3
      • JonBJ JonB

        @H-323
        If you write

        QMediaPlayer player;
        

        the instance only lives as long as the scope it is declared in, and will be destroyed when it goes out of scope. Is that the cause of your error message?

        H.323H Offline
        H.323H Offline
        H.323
        wrote on last edited by H.323
        #3

        @JonB Well I am just trying to understand why this happens cuz that's my main class's constructor and this should works till I close the app which is going to destroy it, is't it? But U have a right if I write this in main it works fine.

        _))

        JonBJ 1 Reply Last reply
        0
        • H.323H H.323

          @JonB Well I am just trying to understand why this happens cuz that's my main class's constructor and this should works till I close the app which is going to destroy it, is't it? But U have a right if I write this in main it works fine.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @H-323
          You need to show us precisely where that QMediaPlayer player; line is in your code.

          H.323H 1 Reply Last reply
          0
          • JonBJ JonB

            @H-323
            You need to show us precisely where that QMediaPlayer player; line is in your code.

            H.323H Offline
            H.323H Offline
            H.323
            wrote on last edited by H.323
            #5

            @JonB __CPP_INIT(GHOST_PLAYER)(const QApplication& Self, QWidget* Parent)
            : QWidget(Parent)
            {
            // setupUi(this);

            QMediaPlayer* player = new QMediaPlayer;

            player->setMedia(QUrl::fromLocalFile("/home/standby/LG7Y.mp3"));
            player->setVolume(50);
            player->play();
            

            }

            __CPP_DEINIT(GHOST_PLAYER){

            }

            int main(int argc, char** argv){
            QApplication GhostPlayer(argc, argv);
            GhostPlayer.setOrganizationName(APP_VENDOR);
            GhostPlayer.setOrganizationDomain(APP_LINK);
            GhostPlayer.setApplicationDisplayName(APP_NAME);
            GhostPlayer.setApplicationName(APP_NAME);
            GhostPlayer.setApplicationVersion(APP_VERSION);

            //-------------------------------------------------------------------------------|
            // Playground
            QMediaPlayer player;
            player.setMedia(QUrl::fromLocalFile("/home/standby/LG7Y.mp3"));
            player.setVolume(50);
            player.play();

            GHOST_PLAYER GPlayer(GhostPlayer);
            GPlayer.setWindowTitle("Ghost Player Version: 0.0.1");
            GPlayer.show();

            //-------------------------------------------------------------------------------|
            return GhostPlayer.exec();
            }

            _))

            JonBJ 1 Reply Last reply
            0
            • H.323H H.323

              @JonB __CPP_INIT(GHOST_PLAYER)(const QApplication& Self, QWidget* Parent)
              : QWidget(Parent)
              {
              // setupUi(this);

              QMediaPlayer* player = new QMediaPlayer;

              player->setMedia(QUrl::fromLocalFile("/home/standby/LG7Y.mp3"));
              player->setVolume(50);
              player->play();
              

              }

              __CPP_DEINIT(GHOST_PLAYER){

              }

              int main(int argc, char** argv){
              QApplication GhostPlayer(argc, argv);
              GhostPlayer.setOrganizationName(APP_VENDOR);
              GhostPlayer.setOrganizationDomain(APP_LINK);
              GhostPlayer.setApplicationDisplayName(APP_NAME);
              GhostPlayer.setApplicationName(APP_NAME);
              GhostPlayer.setApplicationVersion(APP_VERSION);

              //-------------------------------------------------------------------------------|
              // Playground
              QMediaPlayer player;
              player.setMedia(QUrl::fromLocalFile("/home/standby/LG7Y.mp3"));
              player.setVolume(50);
              player.play();

              GHOST_PLAYER GPlayer(GhostPlayer);
              GPlayer.setWindowTitle("Ghost Player Version: 0.0.1");
              GPlayer.show();

              //-------------------------------------------------------------------------------|
              return GhostPlayer.exec();
              }

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @H-323
              OK, if it's not scope then I don't know why it should not work.

              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @JonB said in Help to understand simple thing plz _)):

                then I don't know why it should not work.

                I would guess player.play() maybe needs a running Qt eventloop which is not started here at all.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                JonBJ 1 Reply Last reply
                1
                • Christian EhrlicherC Christian Ehrlicher

                  @JonB said in Help to understand simple thing plz _)):

                  then I don't know why it should not work.

                  I would guess player.play() maybe needs a running Qt eventloop which is not started here at all.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #8

                  @Christian-Ehrlicher
                  But the OP says it works if he uses QMediaPlayer *player = new QMediaPlayer rather than QMediaPlayer player;, why should that matter??

                  1 Reply Last reply
                  0
                  • H.323H Offline
                    H.323H Offline
                    H.323
                    wrote on last edited by
                    #9

                    In this 2 ways it works, but if I write this will not
                    __CPP_INIT(GHOST_PLAYER)(const QApplication& Self, QWidget* Parent)
                    : QWidget(Parent)
                    {
                    // setupUi(this);

                    QMediaPlayer player;

                    player.setMedia(QUrl::fromLocalFile("/home/standby/LG7Y.mp3"));
                    player.setVolume(50);
                    player.play();
                    

                    }

                    _))

                    JonBJ 1 Reply Last reply
                    0
                    • H.323H H.323

                      In this 2 ways it works, but if I write this will not
                      __CPP_INIT(GHOST_PLAYER)(const QApplication& Self, QWidget* Parent)
                      : QWidget(Parent)
                      {
                      // setupUi(this);

                      QMediaPlayer player;

                      player.setMedia(QUrl::fromLocalFile("/home/standby/LG7Y.mp3"));
                      player.setVolume(50);
                      player.play();
                      

                      }

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #10

                      @H-323
                      If you write that QMediaPlayer player; goes out of scope immediately after player.play(); (at the method end }). That's what we said about scope.

                      1 Reply Last reply
                      2
                      • H.323H Offline
                        H.323H Offline
                        H.323
                        wrote on last edited by
                        #11

                        @JonB Okay U have a right I put QMediaPlayer player; into class definition wrote this into constructor and it works now
                        __CPP_INIT(GHOST_PLAYER)(const QApplication& Self, QWidget* Parent)
                        : QWidget(Parent)
                        {
                        // setupUi(this);

                        player.setMedia(QUrl::fromLocalFile("/home/standby/LG7Y.mp3"));
                        player.setVolume(50);
                        player.play();
                        

                        }
                        // Going to read Straustrup again ahaha, thanks guys a lot appreciated so hard for response _))

                        _))

                        1 Reply Last reply
                        1

                        • Login

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