Thread: Music & Sounds With C++

  1. #1
    Registered User
    Join Date
    Aug 2014
    Posts
    26

    Question Music & Sounds With C++

    Problem:
    I need music that repeats/loops in the background while I am running my program. Then, when I get to another part of my program, that background music needs to pause, play another sound file, then resume the background music.

    Example Of Problem:
    For example, I have a Blackjack program that plays the background music when you run it. Then, when you actually log in or create an account it plays a sound file that says "Welcome". Or if i get to a function that shuffles my vector(deck of cards), it plays a sound effect that sounds like a deck of cards shuffling.

    What I Have Tried:

    • Playsound() - It loops/repeats but does not allow me to pause, resume, etc. My code is:

    Code:
            #include <windows.h>        #include <mmsystem.h>
            #pragma comment(lib, "winmm.lib") // Link to the winmm library
    
    
            PlaySound(TEXT("86876__milton__title-screen.wav"), NULL, SND_LOOP | SND_ASYNC); // Background music
    • MCI - Can't figure out how to get it to loop/repeat, pause, or resume. All I can get is the following code:

    Code:
            #include <windows.h>        #include <mmsystem.h>
            #pragma comment(lib, "winmm.lib") // Link to the winmm library
    
    
            MCIERROR me = mciSendString("open 86876__milton__title-screen.wav type waveaudio alias song1",NULL, 0, 0);
            if (me == 0)
            {
                me = mciSendString("play song1", NULL, 0, 0);
            }
    • I keep reading that I need to stay away from 3rd party stuff when working with Visual Studio, or anything Microsoft. But, if you can suggest something that is nice and simple, then please.

    Other:
    I am a beginner so I need it to be as simple as possible. For Python Pygame mixer, it is literally 1 line to play, pause, resume etc. Please help...

  2. #2

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by c_lover View Post
    I keep reading that I need to stay away from 3rd party stuff when working with Visual Studio, or anything Microsoft. But, if you can suggest something that is nice and simple, then please.
    Who told you that? That is absolute nonsense unless someone can prove it is not.
    You should embrace 3rd party stuff. Some of the best things out there come from 3rd party.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Aug 2014
    Posts
    26
    Quote Originally Posted by Elysia View Post
    Who told you that? That is absolute nonsense unless someone can prove it is not.
    You should embrace 3rd party stuff. Some of the best things out there come from 3rd party.
    Documentation on, at least this function, is a little hard to implement unless i search the web for an accompanying example:

    play command (Windows)

    All is has to offer is pretty much some tokens and sometimes outdated code...???

  5. #5
    Registered User
    Join Date
    Aug 2014
    Posts
    26
    Quote Originally Posted by c_lover View Post
    Documentation on, at least this function, is a little hard to implement unless i search the web for an accompanying example:

    play command (Windows)

    All is has to offer is pretty much some tokens and sometimes outdated code...???
    For example, I found the repeat command, but have no idea where to put it.. I tried at the end of the string, but i just doesnt even play now

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by c_lover View Post
    Documentation on, at least this function, is a little hard to implement unless i search the web for an accompanying example:

    play command (Windows)

    All is has to offer is pretty much some tokens and sometimes outdated code...???
    This is Win32 API, which is difficult to use and is old. Very old. Extremely non-portable, non-flexible.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Aug 2014
    Posts
    26
    Does anyone think it would cause some kind of problem down the road if I use Playsound and MCI together? For example, playsound has my looping background music and MCI will do the quick sound effect tidbits that occur throughout the game..??

  8. #8
    Registered User
    Join Date
    Apr 2010
    Posts
    88
    The comments you've heard against mixing Visual Studio with 3rd-party libraries likely stem from the commenters' confusion with setting up Visual Studio projects to use the libraries, or failed attempts to build a library from source using the IDE / command-line. Depending on how good the build documentation or binary support is, it can be tricky to get certain libraries to work. I've read many negative comments about how Windows DLLs work, side by side assemblies, etc. I've found Visual Studio no harder to use than other IDEs I've tried. You just have to get used to it.

    It's hard for the average forum reader/poster to answer that question about mixing libraries unless they know the inner workings of both Playsound and MCI quite well.
    W7, Ubuntu -- mingw, gcc, g++, code::blocks, emacs, notepad++

  9. #9
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by manasij7479 View Post
    And just so you know , I second that especially for a beginner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sounds in C
    By llinocoe in forum C Programming
    Replies: 3
    Last Post: 09-23-2008, 11:21 AM
  2. Ads with sounds
    By ober in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 03-30-2005, 11:38 AM
  3. can someone help with sounds and...
    By Nemini in forum Game Programming
    Replies: 6
    Last Post: 03-17-2005, 10:55 AM
  4. sounds?
    By BODYBUILDNERD in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2002, 03:34 PM
  5. Sounds, or no sounds?
    By face_master in forum C++ Programming
    Replies: 3
    Last Post: 09-03-2001, 05:29 PM

Tags for this Thread