SDL_Mixer 再次播放时,从随机位置开始一段时间然后从头开始
SDL_Mixer when play again, starts from random place for a while then from the beginning
你好,我想在这里触发多首音乐播放,这里有一些代码。
Mix_Music *mix_list[MUSIC_COUNT] ;
//init music with SDL
int result = 0;
int flags = MIX_INIT_MP3;
if (SDL_Init(SDL_INIT_AUDIO) < 0) {
printf("Failed to init SDL\n");
exit(1);
}
if (flags != (result = Mix_Init(flags))) {
printf("Could not initialize mixer (result: %d).\n", result);
printf("Mix_Init: %s\n", Mix_GetError());
exit(1);
}
//load music
Mix_OpenAudio(44100, AUDIO_S16SYS, 2, 640);
for (int i = 0 ; i < musiclist.size() ; ++i){
mix_list[i] = Mix_LoadMUS(musiclist[i].c_str());
}
然后循环,
for (; ;){
//trigger from here, some code detect if there's a new music need to be played
//play sound here
if (!Mix_PlayingMusic()){
//if not playing just start fresh play
std::cout << "Start Play " << musiclist[markerIds[0]] << std::endl ;
Mix_FadeInMusic(mix_list[markerIds[0]],1,1000) ;
}
else{
//only if change to next music
if (lastDetected != markerIds[0]){
std::cout << "Fading out current" << std::endl ;
//first need to fade out current
while(!Mix_FadeOutMusic(2000) && Mix_PlayingMusic()) {
// wait for any fades to complete
SDL_Delay(100);
}
Mix_HaltMusic() ;
//then start the one
//problem happens here
//there will always be several seconds it plays from the middle somewhere, then plays from the beginning.
Mix_FadeInMusic(mix_list[markerIds[0]],1,4000) ;
}
}
}
代码中列出了我的问题,问题是播放之前播放过的音乐时,无论 Mix_FadeInMusic ()
还是 Mix_PlayMusic()
总是先从随机位置播放几秒钟的音乐,然后从头开始。但我所需要的,只是顺利通关。
OS: Ubuntu 16.04
SDL:2.0.4
Mixer:2.0.1
我自己猜的,原来是mp3的问题。 Ubuntu 16.04 捆绑的 SDL 库在播放一些 mp3 文件时几乎没有问题。因此,在我将文件转换为 OGG 并使用 int flags = MIX_INIT_OGG;
之后,问题就消失了。
你好,我想在这里触发多首音乐播放,这里有一些代码。
Mix_Music *mix_list[MUSIC_COUNT] ;
//init music with SDL
int result = 0;
int flags = MIX_INIT_MP3;
if (SDL_Init(SDL_INIT_AUDIO) < 0) {
printf("Failed to init SDL\n");
exit(1);
}
if (flags != (result = Mix_Init(flags))) {
printf("Could not initialize mixer (result: %d).\n", result);
printf("Mix_Init: %s\n", Mix_GetError());
exit(1);
}
//load music
Mix_OpenAudio(44100, AUDIO_S16SYS, 2, 640);
for (int i = 0 ; i < musiclist.size() ; ++i){
mix_list[i] = Mix_LoadMUS(musiclist[i].c_str());
}
然后循环,
for (; ;){
//trigger from here, some code detect if there's a new music need to be played
//play sound here
if (!Mix_PlayingMusic()){
//if not playing just start fresh play
std::cout << "Start Play " << musiclist[markerIds[0]] << std::endl ;
Mix_FadeInMusic(mix_list[markerIds[0]],1,1000) ;
}
else{
//only if change to next music
if (lastDetected != markerIds[0]){
std::cout << "Fading out current" << std::endl ;
//first need to fade out current
while(!Mix_FadeOutMusic(2000) && Mix_PlayingMusic()) {
// wait for any fades to complete
SDL_Delay(100);
}
Mix_HaltMusic() ;
//then start the one
//problem happens here
//there will always be several seconds it plays from the middle somewhere, then plays from the beginning.
Mix_FadeInMusic(mix_list[markerIds[0]],1,4000) ;
}
}
}
代码中列出了我的问题,问题是播放之前播放过的音乐时,无论 Mix_FadeInMusic ()
还是 Mix_PlayMusic()
总是先从随机位置播放几秒钟的音乐,然后从头开始。但我所需要的,只是顺利通关。
OS: Ubuntu 16.04 SDL:2.0.4 Mixer:2.0.1
我自己猜的,原来是mp3的问题。 Ubuntu 16.04 捆绑的 SDL 库在播放一些 mp3 文件时几乎没有问题。因此,在我将文件转换为 OGG 并使用 int flags = MIX_INIT_OGG;
之后,问题就消失了。