Let’s say I only want to play music once, according to the reference documentation I would write this: Mix_PlayMusic(music, 1). But for some reason it plays my music in an endless loop. Even when I set loops to 0 like this: Mix_PlayMusic(music, 0) I get the same result. I don’t have this problem when using Mix_Chunk with Mix_PlayChannel, everything works as advertised.
This is my test code for Mix_PlayMusic:
#include "SDL.h"
#include "SDL_mixer.h"
int main(){
SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL;
SDL_Event event = {0};
SDL_bool is_running = SDL_TRUE;
Mix_Music *music = NULL;
SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO);
Mix_Init(MIX_INIT_MP3|MIX_INIT_OGG);
Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 2, 1024);
SDL_CreateWindowAndRenderer(816, 640, SDL_WINDOW_SHOWN, &window,&renderer);
music = Mix_LoadMUS("Battle1.ogg");
Mix_VolumeMusic(30);
// Still plays music in an infinite loop, despite loop being set to 0 here...
Mix_PlayMusic(music, 0);
while(is_running){
while(SDL_PollEvent(&event)){
switch(event.type){
case SDL_QUIT:
is_running = SDL_FALSE;
break;
}
}
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
}
Mix_FreeMusic(music);
Mix_CloseAudio();
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
Mix_Quit();
SDL_Quit();
return 0;
}
This is my test code for Mix_PlayChannel:
#include "SDL.h"
#include "SDL_mixer.h"
int main(){
SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL;
SDL_Event event = {0};
SDL_bool is_running = SDL_TRUE;
Mix_Chunk *chunk = NULL;
SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO);
Mix_Init(MIX_INIT_MP3|MIX_INIT_OGG);
Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 2, 1024);
SDL_CreateWindowAndRenderer(816, 640, SDL_WINDOW_SHOWN, &window,&renderer);
chunk = Mix_LoadWAV("Computer.ogg");
Mix_Volume(-1, 30);
Mix_PlayChannel(-1, chunk, 1);
while(is_running){
while(SDL_PollEvent(&event)){
switch(event.type){
case SDL_QUIT:
is_running = SDL_FALSE;
break;
}
}
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
}
Mix_FreeChunk(chunk);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
Mix_CloseAudio();
Mix_Quit();
SDL_Quit();
return 0;
}