Jump to content
  • Advertisement
Sign in to follow this  
TakeV

SDL_Mixer

This topic is 3999 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello all. Recently, I've been trying to learn to use SDL, using the tutorials found here: http://lazyfoo.net/SDL_tutorials/index.php Thus far, it's been working quite well, and most of the coding I've been doing using the tutorials as more as a guide. However, I seem to be having an issue when it comes to SDL_Mixer. Now, the program itself, while running, runs perfectly. However, this only works when the song I load for the music is a relatively small wav file. Then I attempted to see if it works, using a large MP3 file. This seems to break things. What happens is, the program itself runs fine, but when I attempt to exit out of the program, it will not work. The program just locks up, and I have to go and kill the program in task manager. I get the feeling that the issue is due to the song still being loaded into memory or something of the sort when I attempt to exit out, but I can't be sure. Hmm, I do not know the tags that allow me to post my code. If there are no complaints, I will post it without said tags. Sorry. EDIT: On second thought, that would ruin the formating. What are the tags?

Share this post


Link to post
Share on other sites
Advertisement
The tags would be [ source lang="cpp" ] [ /source ].

First you need to be sure to use the most current version of SDL_mixer and SDL. A previous version of SDL_mixer had some problems and crashed the app when loading ogg and maybe also mp3 files.

AFAIK for oggs and mp3s you should use the music functions (here). You may have to halt playing music with Mix_HaltMusic and free your music chunk with Mix_FreeMusic.

Share this post


Link to post
Share on other sites
Yes, I believe that I am using the most current version of SDL_Mixer.


However, on another tangent, here is my code.

 

#include <cstdlib>
#include <iostream>
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_mixer.h"
#include "SDL/SDL_ttf.h"

using namespace std;

SDL_Surface *screen = NULL;
SDL_Surface *background = NULL;
SDL_Surface *message = NULL;

TTF_Font *font = NULL;

Mix_Music *music = NULL;

Mix_Chunk *scratch = NULL;
Mix_Chunk *low = NULL;
Mix_Chunk *med = NULL;
Mix_Chunk *high = NULL;

int Init(){
if(SDL_Init(SDL_INIT_EVERYTHING)<0){
cout << SDL_GetError();
return 0;
}

screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);

if(screen == NULL){
cout << SDL_GetError();
return 0;
}

if(TTF_Init() < 0){
cout << SDL_GetError();
return 0;
}
if(Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 4096) < 0){
cout << SDL_GetError();
return 0;
}

return 1;
}

void DrawSurface(SDL_Surface *surface, SDL_Surface *target, int x, int y){
SDL_Rect rect;
rect.x = x;
rect.y = y;
SDL_BlitSurface(surface, NULL, target, &rect);
}


SDL_Surface *LoadImage(std::string filename){
SDL_Surface * opImg = IMG_Load(filename.c_str());
if(opImg!=NULL){
opImg = SDL_DisplayFormat(opImg);
}
return opImg;
}

int LoadFiles(){
background = LoadImage("background.png");
font = TTF_OpenFont("lazy.ttf", 30);
if(background == NULL)
return 0;
if(font == NULL)
return 0;
music = Mix_LoadMUS("largefile.mp3");
if(music == NULL)
return 0;
scratch = Mix_LoadWAV("scratch.wav");
high = Mix_LoadWAV("high.wav");
med = Mix_LoadWAV("medium.wav");
low = Mix_LoadWAV("low.wav");

return 1;
}

void clean_up() {
//Free the surfaces
SDL_FreeSurface( background );
SDL_FreeSurface(message);
//Free the sound effects
Mix_FreeChunk( scratch );
Mix_FreeChunk( high );
Mix_FreeChunk( med );
Mix_FreeChunk( low );
//Free the sound effects
Mix_FreeMusic( music );
//Close the font
TTF_CloseFont( font );
SDL_Quit();
TTF_Quit();
Mix_CloseAudio();
}

void paint(){
DrawSurface(background, screen, 0, 0);
DrawSurface(message, screen, 10, 240);
SDL_Flip(screen);
// message = NULL;
}

int main(int argc, char *argv[])
{
int done = 0;
if(!Init())
return -1;

LoadFiles();

SDL_Color fontColor = {0, 0, 0};

message = TTF_RenderText_Solid(font, "Press 1, 2, 3, 4 or 9 to hear a sound.", fontColor);

// DrawSurface(background, screen, 0, 0);
DrawSurface(message, screen, 10, 240);

SDL_Event event;

while(done == 0){
while(SDL_PollEvent(&event)){
if(event.type == SDL_KEYDOWN){
switch(event.key.keysym.sym){
case SDLK_1:
Mix_PlayChannel(-1, scratch, 0);
message = TTF_RenderText_Solid(font, "Scratch.", fontColor);
break;
case SDLK_2:
Mix_PlayChannel(-1, high, 0);
message = TTF_RenderText_Solid(font, "High.", fontColor);
break;
case SDLK_3:
Mix_PlayChannel(-1, med, 0);
message = TTF_RenderText_Solid(font, "Med.", fontColor);
break;
case SDLK_4:
Mix_PlayChannel(-1, low, 0);
message = TTF_RenderText_Solid(font, "Low.", fontColor);
break;
case SDLK_9:
if(Mix_PlayingMusic()==0){
Mix_PlayMusic(music, -1);
message = TTF_RenderText_Solid(font, "Music on.", fontColor);
}else{
if(Mix_PausedMusic() == 1){
Mix_ResumeMusic();
message = TTF_RenderText_Solid(font, "Resume music.", fontColor);
}else{
Mix_PauseMusic();
message = TTF_RenderText_Solid(font, "Pause music.", fontColor);
}
}
break;
case SDLK_0:
Mix_HaltMusic();
message = TTF_RenderText_Solid(font, "Stop music.", fontColor);
break;
case SDLK_ESCAPE:
while(Mix_PlayingMusic())
Mix_HaltMusic();
done = 1;
break;
}
}
if(event.type == SDL_QUIT){
while(Mix_PlayingMusic())
Mix_HaltMusic();
done = 1;
}
}
paint();


}

clean_up();
return 0;
}





EDIT: To clarify, the program only hangs and crashes when I attempt to exit, either by closing the window, or pressing escape. Nothing breaks at any other time.

[Edited by - TakeV on October 31, 2007 11:44:57 PM]

Share this post


Link to post
Share on other sites
ideas*

1.) run through the program with the debugger and make sure that the music is being freed.
2.) set music to NULL after calling Mix_FreeMusic in the clean up code
3.) call clean_up(); for SDLK_ESCAPE and SDL_QUIT
4.) call Mix_HaltMusic explicitly when exiting.

Share this post


Link to post
Share on other sites
Alright, thanks for the feedback. I tried all those ideas except number one, as I'm still trying to figure out how to work the debugger that comes with Dev-c++.

Interesting to note is that I attempted to run the file with the example code given here: http://lazyfoo.net/SDL_tutorials/lesson11/index.php

Yet this has the same problem.

Share this post


Link to post
Share on other sites
Switching to ogg actually solved the problem completely. I wonder what it is about mp3 that would do that.

Regardless, thank you so very much. You just fixed about a weeks worth of frustration. :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!