Mix loadwav animation freezes when playing for the first time?
Will it pause
use play channel
test
Mix loadwav animation freezes when playing for the first time?
Will it pause
use play channel
test
I don’t know how long it will take to load this૮₍ɵ̷﹏ɵ̷̥̥᷅₎ა
Do you play different sounds on different channels?
SDL2_mixer/Mix_PlayChannel - SDL Wiki
If a specific channel was requested, and there is a chunk already playing there, that chunk will be halted and the new chunk will take its place.
Or do you play more sounds than the number of channels? (default number of channels is 8)
If the specified channel is -1, play on the first free channel (and return -1 without playing anything new if no free channel was available).
struct MyTrack {
std::vector<int> channels ;
Mix_Chunk* wav = nullptr;
};
class AudioEngine {
float speed;
std::unordered_map<std::string, MyTrack*> wav_chunks;
bool play_wav(const std::string &wav_path)
{
auto it = wav_chunks.find(wav_path);
if (it != wav_chunks.end())
{
int channel = Mix_PlayChannelTimed(-1, it->second->wav, 0, -1);
it->second->channels.push_back(channel);
// Custom_Mix_RegisterPlaybackSpeedEffect(channel, it->second->wav, &speed, 1, 0);
return true;
}
MyTrack *mpt = new MyTrack;
mpt->wav = Mix_LoadWAV(wav_path.c_str());
// SDL_Log("%s", SDL_GetError());
wav_chunks[wav_path] = mpt;
int channel = Mix_PlayChannel(-1,mpt->wav,0);// (Mix_PlayChannelTimed(-1, mpt->wav, 0, -1);
mpt->channels.push_back(channel);
return true;
}
public:
static AudioEngine* getInstace() {
static AudioEngine _instance;
return &_instance;
}
AudioEngine() {
Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, MIX_DEFAULT_CHANNELS, 4096);
// Mix_QuerySpec(&audio_frequency, &audio_format, &audio_channel_count); // 查询规格
// audio_allocated_mix_channels_count = Mix_AllocateChannels(MIX_CHANNELS);
speed = 1;
}
This is my way of playing
There is no lag in this sentence
if (it != wav_chunks.end())
{
int channel = Mix_PlayChannelTimed(-1, it->second->wav, 0, -1);
it->second->channels.push_back(channel);
// Custom_Mix_RegisterPlaybackSpeedEffect(channel, it->second->wav, &speed, 1, 0);
return true;
}
Every time it replays, the one below gets stuck
MyTrack *mpt = new MyTrack;
mpt->wav = Mix_LoadWAV(wav_path.c_str());
// SDL_Log("%s", SDL_GetError());
wav_chunks[wav_path] = mpt;
int channel = Mix_PlayChannel(-1,mpt->wav,0);// (Mix_PlayChannelTimed(-1, mpt->wav, 0, -1);
mpt->channels.push_back(channel);
return true;
I noticed that you use float
to store the result of SDL_GetTicks()
. This is not related to your sound problem but I just want to warn you that float
cannot store all integers exactly above 16777216. This means that after a little more than four and a half hour your float
timestamps will start to be imprecise, and it will get worse over time.
oh. ok .I’ll make modifications
I thought you meant the problem was with the music that we could hear in the video (that it stops and are silent for periods of time) but you mean the problem at 0:17 where the graphics animation freezes?
Well, Mix_LoadWAV
can be slow, especially if the duration is long and the file has to be read into memory from a slow HDD. I recommend that you preload all sounds (and other resources) at the start of the game or at the start of each level instead.
Another option is to load the sounds in another thread to avoid blocking the main thread, assuming it’s safe to call Mix_LoadWAV
from a different thread which I’m not sure about.
I see. Thank you for your prompt
I have been using this and it works well. I haven’t found any problems yet
class AudioEngine{
std::mutex _play2dMutex;
bool play_wav(const std::string &wav_path)
{
_play2dMutex.lock();
loadwav
Mix_PlayChannelTimed
...
_play2dMutex.unlock();
}
other function
std::thread(&AudioEngine::play_wav, this, wav_path).detach();