Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Help to understand simple thing plz _))



  • // 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. _))



  • @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-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?



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



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



  • @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();
    }



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


  • Qt Champions 2019

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



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



  • 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();
    

    }



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



  • @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 _))


Log in to reply