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

QMediaPlayer



  • Dear QTDevs,
    I am struggeling with the QMediaPlayer. I can play songs and stop them. But the song has 40s and I want to use it as a notification sound. So i should only play for 8 sec and then stops. I tried some things with sleep() or delay. But it is not working and i thought there would be a way to do it more neat.

    Thanks a lot,
    Me


  • Lifetime Qt Champion

    Hi,

    For that kind of usage QSound should be used. Take a look at the audio file type constraints.



  • What command is it there cant find anything
    Thx


  • Lifetime Qt Champion

    @Malaluigi

    Hi
    There is no PlayPart or similar. You will have to call stop on it when you want it to stop before its
    actual duration.

    Some way could be.
    Note the use of static. if you only need this in one Form
    i would make QSound s a member and the function a member also.
    However, this is contained in a function using a lambda for compactness.

    void PlayNoti(int duration = 8000) /*msec*/
    {
        static QSound s("mysounds/bells.wav");
        s.play();
        QTimer::singleShot(duration, []() {
            s.stop();
        });
    }
    
    


  • thanks mate !!!!
    LGTM


Log in to reply