Thread: Adding music to a game

  1. #1
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428

    Adding music to a game

    I already got the first level of the game working including sound bits for different things, but as soon as I tried to have music play the entire game I ran into a problem. There is nothing wrong with the code it compiles fine, but it won't run the music. No matter where I put the

    Code:
    if( Mix_PlayingMusic() == 0 )
                        {
                            //Play the music
                            if( Mix_PlayMusic( music, -1 ) == -1 )
                            {
                                return 1;
                            }    
                        }
    The game freezes as soon as it hits this function. The sound file is 1.5 mb and I am running on windows XP. Any insights? Thank you for your time.

  2. #2
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    try this..

    Code:
    if(Mix_PlayMusic(music, -1)==-1) {
        printf("Mix_PlayMusic: %s\n", Mix_GetError());
    }

  3. #3
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    Ok now the game will play regardless, but why won't the music play? It is the correct filename.

  4. #4
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    Did you get an error from Spidey's suggestion?

  5. #5
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    no error occured and the game doesn't freeze anymore... but the music still doesn't play. Is there a compatability issue between SDL_mixer and WinXP?

  6. #6
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    no error occured and the game doesn't freeze anymore... but the music still doesn't play. Is there a compatability issue between SDL_mixer and WinXP?
    None that I know of. Try breaking it down, Heres a sample I have tested on my computer..

    Code:
    Mix_Music *music = NULL;
    music = Mix_LoadMUS("music.wav");
    if(!music )
    {
    fprintf(stderr, "Couldn't load %s: %s\n", "music.wav",  
    Mix_GetError());
    }
    Mix_PlayMusic(music, 5);
    or try this..

    Code:
    int MusicVolume = 128;
    if (Mix_PlayingMusic() == 1)   Mix_HaltMusic();
    SDL_Delay(1000);
     
    Mix_VolumeMusic(MusicVolume);
     
    if(Mix_PlayMusic(music,  -1)==-1)
    {
    printf("Mix_PlayMusic: %s\n", Mix_GetError());
    }
    try it with a different file if this doesn't work. I have had times when certain files have failed for no apparent reason.

  7. #7
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    Tried all three I am at a loss. I already finished the game and moved on. Remeber how the game locked up until I took it out of the while playing music loop. I am assuming that I am just not successfully playing the music on my computer causing it to continually loop trying to unsuccessfully play the music. That was why I assumed I had a compatability issue, but no one seems to have heard of this. After I removed the while loop the game played fine, but there was no music. I guess after I finish the project I am working on tonight I will start a new file devoted to figuring this out. Trying multiple file sizes and stuff. If I stumble upon the answer I will post back to let people know. I appreciate your time spidey and carrotcake.

    Ohh and the printf function.... where does that text go? This might allow me find further information about the error.

  8. #8
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Ohh and the printf function.... where does that text go? This might allow me find further information about the error.
    You'll have to run it through command line.

  9. #9
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    Well I spent most of this morning using every different way I can think of to initialize music. Starting new files downloaded multiple .wavs of different size. Phrasing the initialize different ways. I just can't get music to initialize. No matter the file size or name. Its very frustrating. I've only used SDL for a couple days so I will wait a while maybe revisit this problem in a couple weeks. If anyone could write a short source code that will load music for them and attach it so I can copy paste and change the file name to see if it is just phrasing that is incorrect I would appreciate it. (Though it might take away from my problem solving skills) Otherwise I have givin up for now lol.

    Current Score
    SDL_Mixer: 30
    Lesshardtofind: 0

  10. #10
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Here, try this tutorial. It might help you figure out your bug as well.

    SDL_Mixer Tutorial

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C# - Building a SCUMM-like game.. questions
    By Iyouboushi in forum Game Programming
    Replies: 0
    Last Post: 05-24-2008, 10:54 PM
  2. my upcoming UNO card game :)
    By Hussain Hani in forum Game Programming
    Replies: 5
    Last Post: 01-24-2008, 01:19 AM
  3. Please comment on my c++ game
    By MegaManZZ in forum Game Programming
    Replies: 10
    Last Post: 01-22-2008, 11:03 AM
  4. u got me wrong fellas
    By clover in forum Game Programming
    Replies: 4
    Last Post: 10-18-2003, 04:23 PM
  5. FXSeries
    By jverkoey in forum Game Programming
    Replies: 6
    Last Post: 10-14-2003, 08:06 PM

Tags for this Thread