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 add sound when window opens?
Forum Updated to NodeBB v4.3 + New Features

How to add sound when window opens?

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 2.9k 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.
  • H Offline
    H Offline
    hubris0010
    wrote on last edited by
    #1

    I've tried making it so that the sound will start when the window before it closes by pushing a button but it really doesn't work at all. I just want to find out how to autoplay a mp3 file. I have no clue how to do so. Can anyone help?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      MuldeR
      wrote on last edited by
      #2

      Maybe QSound?

      But it has some limitation, such that it can't play a sound file you include into your app as resource.

      Therefore I tend to use PlaySound() directly, on Win32.

      My OpenSource software at: http://muldersoft.com/

      Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

      Go visit the coop: http://youtu.be/Jay...

      1 Reply Last reply
      0
      • H Offline
        H Offline
        hubris0010
        wrote on last edited by
        #3

        I'm on a mac so I can't use any functions strictly for windows. Plus I'd prefer if I could keep this cross-platform. I tried QSound and used the code
        @ QSound music("/sound/08 West Mansion.mp3");
        music.play();@

        However that didn't work quite right. It makes no sound when I press the button that it should be associated with.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          MuldeR
          wrote on last edited by
          #4

          The QSound documentation says:

          bq. Mac OS X: NSSound is used. All formats that NSSound supports, including QuickTime formats, are supported by Qt for Mac OS X.

          So I think on Mac OS X playing MP3 files should work. But maybe AAC/MP4 would be more common these days. This is in contrast to Windows, where only WAVE files are supported by QSound.

          My OpenSource software at: http://muldersoft.com/

          Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

          Go visit the coop: http://youtu.be/Jay...

          1 Reply Last reply
          0
          • H Offline
            H Offline
            hubris0010
            wrote on last edited by
            #5

            Well I'm working with my friend on this so I need it to be completely os independent cause he's on a pc. Unless maybe I make two seperate blocks of code? One for mac and one for pc. Giving each its own code respectively. Now I'm kinda just bouncing ideas off of you. Would you mind showing me how you'd do this with PlaySound()?

            1 Reply Last reply
            0
            • M Offline
              M Offline
              MuldeR
              wrote on last edited by
              #6

              Using PlaySound is straight forward:
              @#include <Windows.h>
              #include <MMSystem.h>
              PlaySound(MAKEINTRESOURCE(IDR_WAVE_FOO), GetModuleHandle(NULL), SND_RESOURCE | SND_ASYNC)@

              Here IDR_WAVE_FOO is the Resource Id of the Wave file we want to play. But this refers to Win32 resources, which are completely separate from Qt's resource system. But both can be used in the same binary just fine.

              But if you rneed something that really works platfrom-independant, you might call MPlayer as an external process via QProcess. You need a separate MPlayer binary for each platfrom, but that shouldn't be a problem...

              My OpenSource software at: http://muldersoft.com/

              Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

              Go visit the coop: http://youtu.be/Jay...

              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