Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Game Development
  4. How to play sounds using QMediaPlayer
Forum Updated to NodeBB v4.3 + New Features

How to play sounds using QMediaPlayer

Scheduled Pinned Locked Moved Solved Game Development
16 Posts 4 Posters 3.5k Views 1 Watching
  • 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.
  • 8Observer88 8Observer8

    My example from the topic works with Qt5 but doesn't work with Qt6.

    C Offline
    C Offline
    CPPUIX
    wrote on last edited by CPPUIX
    #7

    @8Observer8

    From Changes to Qt Multimedia in Qt 6:

    Audio inputs and output QMediaPlayer and QMediaCaptureSession (and the corresponding QML types MediaPlayer and CaptureSession) are not connected to any audio devices by default. Explicitly connect them to a QAudioInput/AudioInput or QAudioOutput/AudioOutput to capture or play back audio.

    You're not doing that in your code and I forgot to mention that I did so in my test; something like:

    mediaPlayer.setAudioOutput(new QAudioOutput);
    

    If this still does not fix it, try an absolute path just to verify it's a file access issue or not.

    8Observer88 1 Reply Last reply
    1
    • C CPPUIX

      @8Observer8

      From Changes to Qt Multimedia in Qt 6:

      Audio inputs and output QMediaPlayer and QMediaCaptureSession (and the corresponding QML types MediaPlayer and CaptureSession) are not connected to any audio devices by default. Explicitly connect them to a QAudioInput/AudioInput or QAudioOutput/AudioOutput to capture or play back audio.

      You're not doing that in your code and I forgot to mention that I did so in my test; something like:

      mediaPlayer.setAudioOutput(new QAudioOutput);
      

      If this still does not fix it, try an absolute path just to verify it's a file access issue or not.

      8Observer88 Offline
      8Observer88 Offline
      8Observer8
      wrote on last edited by 8Observer8
      #8

      @Abderrahmene_Rayene said in How to play sounds using QMediaPlayer:

      new QAudioOutput

      Yes, it helped! Thank you very much! Interesting that this didn't work with qrc:// and works with qrc:/

      #include "widget.h"
      #include <QtCore/QUrl>
      #include <QtWidgets/QPushButton>
      #include <QtMultimedia/QAudioOutput>
      
      Widget::Widget(QWidget *parent)
          : QWidget(parent)
      {
          mediaPlayer.setSource(QUrl("qrc:/assets/audio/music.wav"));
          mediaPlayer.setAudioOutput(new QAudioOutput);
      
          QPushButton *playButton = new QPushButton("Play", this);
          connect(playButton, &QPushButton::pressed, this, &Widget::play);
      
          connect(&mediaPlayer, &QMediaPlayer::mediaStatusChanged, this, [this] (QMediaPlayer::MediaStatus status) -> void  {
              qDebug() << "QMediaPlayer::mediaStatusChanged" << status;
          });
      }
      
      Widget::~Widget()
      {
      }
      
      void Widget::play()
      {
          mediaPlayer.play();
          qDebug() << "play";
      }
      
      
      JoeCFDJ 1 Reply Last reply
      0
      • 8Observer88 8Observer8 has marked this topic as solved on
      • 8Observer88 8Observer8

        @Abderrahmene_Rayene said in How to play sounds using QMediaPlayer:

        new QAudioOutput

        Yes, it helped! Thank you very much! Interesting that this didn't work with qrc:// and works with qrc:/

        #include "widget.h"
        #include <QtCore/QUrl>
        #include <QtWidgets/QPushButton>
        #include <QtMultimedia/QAudioOutput>
        
        Widget::Widget(QWidget *parent)
            : QWidget(parent)
        {
            mediaPlayer.setSource(QUrl("qrc:/assets/audio/music.wav"));
            mediaPlayer.setAudioOutput(new QAudioOutput);
        
            QPushButton *playButton = new QPushButton("Play", this);
            connect(playButton, &QPushButton::pressed, this, &Widget::play);
        
            connect(&mediaPlayer, &QMediaPlayer::mediaStatusChanged, this, [this] (QMediaPlayer::MediaStatus status) -> void  {
                qDebug() << "QMediaPlayer::mediaStatusChanged" << status;
            });
        }
        
        Widget::~Widget()
        {
        }
        
        void Widget::play()
        {
            mediaPlayer.play();
            qDebug() << "play";
        }
        
        
        JoeCFDJ Offline
        JoeCFDJ Offline
        JoeCFD
        wrote on last edited by JoeCFD
        #9

        @8Observer8 said in How to play sounds using QMediaPlayer:

        connect(&mediaPlayer, &QMediaPlayer::mediaStatusChanged, this, [this] (QMediaPlayer::MediaStatus status) -> void  {
            qDebug() << "QMediaPlayer::mediaStatusChanged" << status;
        });
        

        Does the following work?

        connect(&mediaPlayer, &QMediaPlayer::mediaStatusChanged, 
                [=](QMediaPlayer::MediaStatus status) -> void{ qDebug() << "QMediaPlayer::mediaStatusChanged" << status;}
               );
        
        8Observer88 1 Reply Last reply
        1
        • JoeCFDJ JoeCFD

          @8Observer8 said in How to play sounds using QMediaPlayer:

          connect(&mediaPlayer, &QMediaPlayer::mediaStatusChanged, this, [this] (QMediaPlayer::MediaStatus status) -> void  {
              qDebug() << "QMediaPlayer::mediaStatusChanged" << status;
          });
          

          Does the following work?

          connect(&mediaPlayer, &QMediaPlayer::mediaStatusChanged, 
                  [=](QMediaPlayer::MediaStatus status) -> void{ qDebug() << "QMediaPlayer::mediaStatusChanged" << status;}
                 );
          
          8Observer88 Offline
          8Observer88 Offline
          8Observer8
          wrote on last edited by
          #10

          @JoeCFD Yes! Thanks a lot!

          1 Reply Last reply
          0
          • 8Observer88 8Observer8 referenced this topic on
          • 8Observer88 Offline
            8Observer88 Offline
            8Observer8
            wrote on last edited by 8Observer8
            #11

            Hello guys again. I want to rewrite the example above to PySide6 and PyQt6:

            from PySide6.QtCore import QUrl
            from PySide6.QtMultimedia import QAudioOutput, QMediaPlayer
            from PySide6.QtWidgets import QPushButton, QWidget
            
            
            class Widget(QWidget):
                def __init__(self):
                    super().__init__()
            
                    self.mediaPlayer = QMediaPlayer()
                    self.mediaPlayer.setSource(QUrl("assets/audio/music.wav"))
                    self.mediaPlayer.setAudioOutput(QAudioOutput())
                    self.mediaPlayer.mediaStatusChanged.connect(lambda status : print(status))
            
                    playButton = QPushButton("Play", self)
                    playButton.clicked.connect(self.play)
            
                def play(self):
                    self.mediaPlayer.play()
                    print("play")
            

            main.py

            import sys
            
            from PySide6.QtCore import Qt
            from PySide6.QtWidgets import QApplication
            
            from widget import Widget
            
            if __name__ == "__main__":
                app = QApplication(sys.argv)
                w = Widget()
                w.show()
                sys.exit(app.exec())
            

            But I have these errors:

            MediaStatus.InvalidMedia
            MediaStatus.LoadingMedia
            play
            MediaStatus.InvalidMedia

            The path to the audio file is correct:

            9b33ec0f-7d20-4871-8f12-42d3a0b02366-image.png

            JonBJ 1 Reply Last reply
            0
            • 8Observer88 Offline
              8Observer88 Offline
              8Observer8
              wrote on last edited by
              #12

              I tried to use fromLocalFile but I have the same result:

              self.mediaPlayer.setSource(QUrl.fromLocalFile("assets/audio/music.wav"))
              
              1 Reply Last reply
              0
              • 8Observer88 8Observer8

                Hello guys again. I want to rewrite the example above to PySide6 and PyQt6:

                from PySide6.QtCore import QUrl
                from PySide6.QtMultimedia import QAudioOutput, QMediaPlayer
                from PySide6.QtWidgets import QPushButton, QWidget
                
                
                class Widget(QWidget):
                    def __init__(self):
                        super().__init__()
                
                        self.mediaPlayer = QMediaPlayer()
                        self.mediaPlayer.setSource(QUrl("assets/audio/music.wav"))
                        self.mediaPlayer.setAudioOutput(QAudioOutput())
                        self.mediaPlayer.mediaStatusChanged.connect(lambda status : print(status))
                
                        playButton = QPushButton("Play", self)
                        playButton.clicked.connect(self.play)
                
                    def play(self):
                        self.mediaPlayer.play()
                        print("play")
                

                main.py

                import sys
                
                from PySide6.QtCore import Qt
                from PySide6.QtWidgets import QApplication
                
                from widget import Widget
                
                if __name__ == "__main__":
                    app = QApplication(sys.argv)
                    w = Widget()
                    w.show()
                    sys.exit(app.exec())
                

                But I have these errors:

                MediaStatus.InvalidMedia
                MediaStatus.LoadingMedia
                play
                MediaStatus.InvalidMedia

                The path to the audio file is correct:

                9b33ec0f-7d20-4871-8f12-42d3a0b02366-image.png

                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by
                #13

                @8Observer8

                QUrl("assets/audio/music.wav")

                Test with an absolute path rather than a relative one. Not sure what the current directory is when running Python script, so check this is not the issue?

                8Observer88 1 Reply Last reply
                0
                • JonBJ JonB

                  @8Observer8

                  QUrl("assets/audio/music.wav")

                  Test with an absolute path rather than a relative one. Not sure what the current directory is when running Python script, so check this is not the issue?

                  8Observer88 Offline
                  8Observer88 Offline
                  8Observer8
                  wrote on last edited by
                  #14

                  @JonB I tried, but no, it doesn't work. The result is the same as above

                  1 Reply Last reply
                  0
                  • 8Observer88 Offline
                    8Observer88 Offline
                    8Observer8
                    wrote on last edited by 8Observer8
                    #15

                    But if I write the wrong path, I get a different error message:

                    self.mediaPlayer.setSource(QUrl("wrong_path/audio/music.wav"))
                    
                    handleSourceError: 0x80070003
                    MediaStatus.InvalidMedia
                    MediaStatus.LoadingMedia
                    play
                    handleSourceError: 0x80070003
                    MediaStatus.InvalidMedia
                    
                    1 Reply Last reply
                    0
                    • 8Observer88 Offline
                      8Observer88 Offline
                      8Observer8
                      wrote on last edited by
                      #16

                      I solved the problem! I found the solution here: https://zhuanlan.zhihu.com/p/521028351

                              self.audioOutput = QAudioOutput()
                              self.mediaPlayer.setAudioOutput(self.audioOutput)
                      
                      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