First of all, a detail: by the description of your problem, the type Mix_Music*
seems more appropriate than the guy Mix_Chunk*
. Anyway, I’m going to answer the question based on the type Mix_Chunk*
.
Regarding the use of std::thread
, it seems to me unnecessary, because the function Mix_PlayChannel
already runs the song in a separate thread. So, what you could do is use the function Mix_Playing()
to know if a song is being played or not and variables to control which song is being played, what’s next, etc.
I did a function to accomplish this task, called Player()
and how I wanted to test I prepared the function main()
also. I put both below because I think it makes it easier for you to understand what has been done:
#include <iostream>
#include <SDL.h>
#include <SDL_mixer.h>
void Player(Mix_Chunk* mMusic[])
{
static int previous = 0;
static int current = 0;
if (current < 4) {
if (previous == current) {
std::cout << "Tocando musica n. " << current + 1 << '\n';
Mix_PlayChannel(-1, mMusic[current], 1);
current++;
}
if (!Mix_Playing(-1)) {
previous = current;
}
}
}
int main(int argc, char* argv[])
{
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO);
SDL_Window* window = SDL_CreateWindow("Title",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
960, 540, SDL_WINDOW_SHOWN);
if (!window) { return -1; }
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1,
SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
SDL_SetRenderDrawColor(renderer, 50, 50, 50, 255);
SDL_RenderClear(renderer);
//Initialize SDL_mixer
Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048);
//The music that will be played
Mix_Chunk *gMusic = Mix_LoadWAV("beat.wav");
Mix_Chunk* mMusic[4] = { gMusic, gMusic, gMusic, gMusic };
SDL_Event e;
int previous = 1;
int current = 1;
while (true) {
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT) {
return 0;
}
}
Player(mMusic);
SDL_RenderPresent(renderer);
}
//Free the music
Mix_FreeChunk(gMusic);
gMusic = NULL;
//Destroy window
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
window = NULL;
renderer = NULL;
//Quit SDL subsystems
Mix_Quit();
SDL_Quit();
return 0;
}
Thanks, V. Santos. Thank you, I’ll test here and come back to post to see if it worked... It is that in fact, I use a form - Win Form... and putting any kind of while the form crashed, did not respond to commands. Actually I decided to put the audio in a thread and using detach() instead of Join... the thread actually runs in parallel and the other things in the program keep responding... but I’ll test your suggestion. If it works, better I don’t need to use threads...
– mrsoliver