2017-06-12 46 views
-1

嗨我試圖觸發多個音樂在這裏播放,這裏有一些代碼。SDL_Mixer再次播放時,從隨機地點開始然後從開始

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()始終發揮從音樂隨機放置幾秒鐘,然後從頭開始。但我需要的只是順利播放。

操作系統:Ubuntu的16.04 SDL:2.0.4 混合機:2.0.1

回答

0

我這個想通自己,它實際上是MP3的問題。與Ubuntu 16.04捆綁在一起的SDL庫在播放一些mp3文件方面幾乎沒有問題。所以在我將文件轉換爲OGG並使用int flags = MIX_INIT_OGG;之後,問題就消失了。