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. how to input system music file using QMediaplayer in qt6.2
Forum Update on Monday, May 27th 2025

how to input system music file using QMediaplayer in qt6.2

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 5 Posters 1.1k 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.
  • A Offline
    A Offline
    akikaede
    wrote on last edited by
    #1

    Hi, I'm writing a program that count down time with music playing background in qt6.2, but following detailed description in https://doc-snapshots.qt.io/qt6-dev/qmediaplayer.html,

    player = new QMediaPlayer;
    connect(player, SIGNAL(positionChanged(qint64)), this, SLOT(positionChanged(qint64)));
    player->setSource(QUrl::fromLocalFile("/Users/me/Music/coolsong.mp3"));
    player->setVolume(50);
    player->play();
    

    my music cannot play, and strangely, my qt doesn't have setvolume function, here is my music part code:

    m_player = new QMediaPlayer(this);
    connect(m_player, SIGNAL(positionChanged(qint64)), this, SLOT(positionChanged(qint64)));
    m_player->setSource(QUrl::fromLocalFile("C:/Users/aki/Downloads/bgm1.mp3"));
    m_player->play();
    

    this file location is real and the file is exist, what's wrong about my code?

    1 Reply Last reply
    0
    • A Offline
      A Offline
      akikaede
      wrote on last edited by
      #2

      I find that I need to add QAudioOutput in mediaplayer, so that I can play music in qt.

      A 1 Reply Last reply
      1
      • Y Offline
        Y Offline
        Y.D.X.
        wrote on last edited by
        #3

        Same. It's confusing that I got QMediaPlayer::ResourceError.

        I didn't understand your solution till I read Qt Multimedia in Qt 6:

        QMediaPlayer in Qt 6 requires you to actively connect it to both an audio and video output using the setAudioOutput() and setVideoOutput() methods. Not setting an audio output will imply that the media player doesn’t play audio. This is a change from Qt 5, where a default audio output was always selected.

        (bolds mine)

        Y 1 Reply Last reply
        0
        • Y Y.D.X.

          Same. It's confusing that I got QMediaPlayer::ResourceError.

          I didn't understand your solution till I read Qt Multimedia in Qt 6:

          QMediaPlayer in Qt 6 requires you to actively connect it to both an audio and video output using the setAudioOutput() and setVideoOutput() methods. Not setting an audio output will imply that the media player doesn’t play audio. This is a change from Qt 5, where a default audio output was always selected.

          (bolds mine)

          Y Offline
          Y Offline
          Y.D.X.
          wrote on last edited by
          #4

          Then I met QAudioOutput crash issue.

          However, all I need (this time) is to play a WAV, so I turn to PowerShell. Some thing like this:

          private:
              QProcess *_shell;
          public:
              WavPlayer()
              {
                  this->_shell = new QProcess(this);
                  this->_shell->setProgram("powershell");
                  this->_shell->start();
              }
              void play(const QFileInfo &file)
              {
                  this->_shell->write("(New-Object Media.SoundPlayer '" + file.absoluteFilePath() + "').Play()\r\n".toUtf8());
              }
          

          Note: Details (changing PS's code page, dealing with qrc path, etc.) are removed to make it clear. The above code may not work in real project.

          (Multi-QProcess needed if you want to play several WAV simultaneously)

          1 Reply Last reply
          0
          • B Offline
            B Offline
            BrittanyJacqueline
            wrote on last edited by
            #5
            This post is deleted!
            1 Reply Last reply
            0
            • A akikaede

              I find that I need to add QAudioOutput in mediaplayer, so that I can play music in qt.

              A Offline
              A Offline
              Alireza_ALZ
              wrote on last edited by
              #6

              @akikaede

              @akikaede said in how to input system music file using QMediaplayer in qt6.2:

              I find that I need to add QAudioOutput in mediaplayer, so that I can play music in qt.

              Hi there . I have same problem and i dont know how to use QAudioOutput ! Can you help me please ?

              SGaistS 1 Reply Last reply
              0
              • A Alireza_ALZ

                @akikaede

                @akikaede said in how to input system music file using QMediaplayer in qt6.2:

                I find that I need to add QAudioOutput in mediaplayer, so that I can play music in qt.

                Hi there . I have same problem and i dont know how to use QAudioOutput ! Can you help me please ?

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

                @Alireza_ALZ hi,

                The QMediaPlayer documentation shows a minimal example.

                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

                • Login

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