2015-07-02 82 views
0

我是新來的Android,我已經構建了16步驟模式序列器。我已經使用MediaPlayer完成了這個工作,但是當同時播放超過1個聲音時,它會變慢:/ ..所以我嘗試使用SoundPool(標記爲棄用),但是同樣的效果出現在這裏,太..Android - MediaPlayer/SoundPool性能?

它是建立像: - 每一步,我是否記設置..如果是這樣..播放聲音 - 16步循環運行在一個線程

經過長時間的測試,似乎第一次貫穿(所有16個步驟)都是以正常速度進行..隨着第二次貫穿的開始,所有音量都會根據需要播放多少聲音而減慢。:/

是還有其他解決方案,我可以嘗試?

編輯:

這是我的序碼

初始化:

soundPool = new SoundPool(32, AudioManager.STREAM_MUSIC, 0); 
    soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() { 
     @Override 
     public void onLoadComplete(SoundPool soundPool, int sampleId, int status) { 
      loaded = true; 
     } 
    }); 
    soundID_kick_1 = soundPool.load(this, R.raw.kick1, 1); 
    soundID_kick_2 = soundPool.load(this, R.raw.kick2, 1); 
    soundID_kick_3 = soundPool.load(this, R.raw.kick3, 1); 
    soundID_kick_4 = soundPool.load(this, R.raw.kick4, 1); 
    soundID_kick_5 = soundPool.load(this, R.raw.kick5, 1); 
    soundID_kick_6 = soundPool.load(this, R.raw.kick6, 1); 
    soundID_kick_7 = soundPool.load(this, R.raw.kick7, 1); 
    soundID_kick_8 = soundPool.load(this, R.raw.kick8, 1); 
    soundID_hh_1 = soundPool.load(this, R.raw.hh1, 2); 
    soundID_hh_2 = soundPool.load(this, R.raw.hh2, 2); 
    soundID_hh_3 = soundPool.load(this, R.raw.hh3, 2); 
    soundID_hh_4 = soundPool.load(this, R.raw.hh4, 2); 
    soundID_hh_5 = soundPool.load(this, R.raw.hh5, 2); 
    soundID_hh_6 = soundPool.load(this, R.raw.hh6, 2); 
    soundID_hh_7 = soundPool.load(this, R.raw.hh7, 2); 
    soundID_hh_8 = soundPool.load(this, R.raw.hh8, 2); 
    soundID_sd_1 = soundPool.load(this, R.raw.sd1, 3); 
    soundID_sd_2 = soundPool.load(this, R.raw.sd2, 3); 
    soundID_sd_3 = soundPool.load(this, R.raw.sd3, 3); 
    soundID_sd_4 = soundPool.load(this, R.raw.sd4, 3); 
    soundID_sd_5 = soundPool.load(this, R.raw.sd5, 3); 
    soundID_sd_6 = soundPool.load(this, R.raw.sd6, 3); 
    soundID_sd_7 = soundPool.load(this, R.raw.sd7, 3); 
    soundID_sd_8 = soundPool.load(this, R.raw.sd8, 3); 
    soundID_snd_1 = soundPool.load(this, R.raw.snd1, 4); 
    soundID_snd_2 = soundPool.load(this, R.raw.snd2, 4); 
    soundID_snd_3 = soundPool.load(this, R.raw.snd3, 4); 
    soundID_snd_4 = soundPool.load(this, R.raw.snd4, 4); 
    soundID_snd_5 = soundPool.load(this, R.raw.snd5, 4); 
    soundID_snd_6 = soundPool.load(this, R.raw.snd6, 4); 
    soundID_snd_7 = soundPool.load(this, R.raw.snd7, 4); 
    soundID_snd_8 = soundPool.load(this, R.raw.snd8, 4); 

後點擊seqPlay當序運行:

private View.OnClickListener seqPlay = new View.OnClickListener() { 
    public void onClick(View v) { 
     btn_seq_play = (ImageButton)findViewById(R.id.btn_seq_play); 
     Thread seq_thread = new Thread(new Runnable() { 
      @Override 
      public void run() { 
       // Do loop while seq_status==true 
       while(seq_status) { 
        for (int i = 0; i <= 15; i++) { 
         // Play Kicks if set 
         Log.i("Durchlauf Kicks" + i, "seqKick_status[i] " + seqKick_status[i]); 
         if (seqKick_status[i] != 0 && !mute_kick_status) { 
          switch (seqKick_status[i]) { 
           case 1: { 
            soundPool.play(soundID_kick_1, volKick[i], volKick[i], 1, 0, 1f);          
            break; 
           } 
           case 2: { 
            soundPool.play(soundID_kick_2, volKick[i], volKick[i], 1, 0, 1f); 
            break; 
           } 
           case 3: { 
            soundPool.play(soundID_kick_3, volKick[i], volKick[i], 1, 0, 1f); 
            break; 
           } 
           case 4: { 
            soundPool.play(soundID_kick_4, volKick[i], volKick[i], 1, 0, 1f); 
            break; 
           } 
           case 5: { 
            soundPool.play(soundID_kick_5, volKick[i], volKick[i], 1, 0, 1f); 
            break; 
           } 
           case 6: { 
            soundPool.play(soundID_kick_6, volKick[i], volKick[i], 1, 0, 1f); 
            break; 
           } 
           case 7: { 
            soundPool.play(soundID_kick_7, volKick[i], volKick[i], 1, 0, 1f); 
            break; 
           } 
           case 8: { 
            soundPool.play(soundID_kick_8, volKick[i], volKick[i], 1, 0, 1f); 
            break; 
           } 
          } 
         } 

         // Play HH if set 
         Log.i("Durchlauf HH" + i, "seqHH_status[i] " + seqHH_status[i]); 
         if (seqHH_status[i] != 0 && !mute_hh_status) { 
          switch (seqHH_status[i]) { 
           case 1: { 
            soundPool.play(soundID_hh_1, volHH[i], volHH[i], 1, 0, 1f); 
            break; 
           } 
           case 2: { 
            soundPool.play(soundID_hh_2, volHH[i], volHH[i], 1, 0, 1f); 
            break; 
           } 
           case 3: { 
            soundPool.play(soundID_hh_3, volHH[i], volHH[i], 1, 0, 1f); 
            break; 
           } 
           case 4: { 
            soundPool.play(soundID_hh_4, volHH[i], volHH[i], 1, 0, 1f); 
            break; 
           } 
           case 5: { 
            soundPool.play(soundID_hh_5, volHH[i], volHH[i], 1, 0, 1f); 
            break; 
           } 
           case 6: { 
            soundPool.play(soundID_hh_6, volHH[i], volHH[i], 1, 0, 1f); 
            break; 
           } 
           case 7: { 
            soundPool.play(soundID_hh_7, volHH[i], volHH[i], 1, 0, 1f); 
            break; 
           } 
           case 8: { 
            soundPool.play(soundID_hh_8, volHH[i], volHH[i], 1, 0, 1f); 
            break; 
           } 

          } 
         } 
         // Play SD if set 
         Log.i("Durchlauf SD" + i, "seqSD_status[i] " + seqSD_status[i]); 
         if (seqSD_status[i] != 0 && !mute_sd_status) { 
          switch (seqSD_status[i]) { 
           case 1: { 
            soundPool.play(soundID_sd_1, volSD[i], volSD[i], 1, 0, 1f); 
            break; 
           } 
           case 2: { 
            soundPool.play(soundID_sd_2, volSD[i], volSD[i], 1, 0, 1f); 
            break; 
           } 
           case 3: { 
            soundPool.play(soundID_sd_3, volSD[i], volSD[i], 1, 0, 1f); 
            break; 
           } 
           case 4: { 
            soundPool.play(soundID_sd_4, volSD[i], volSD[i], 1, 0, 1f); 
            break; 
           } 
           case 5: { 
            soundPool.play(soundID_sd_5, volSD[i], volSD[i], 1, 0, 1f); 
            break; 
           } 
           case 6: { 
            soundPool.play(soundID_sd_6, volSD[i], volSD[i], 1, 0, 1f); 
            break; 
           } 
           case 7: { 
            soundPool.play(soundID_sd_7, volSD[i], volSD[i], 1, 0, 1f); 
            break; 
           } 
           case 8: { 
            soundPool.play(soundID_sd_8, volSD[i], volSD[i], 1, 0, 1f); 
            break; 
           } 
          } 
         } 
         // Play SND if set 
         Log.i("Durchlauf SND" + i, "seqSND_status[i] " + seqSND_status[i]); 
         if (seqSND_status[i] != 0 && !mute_snd_status) { 
          switch (seqSND_status[i]) { 
           case 1: { 
            soundPool.play(soundID_snd_1, volSND[i], volSND[i], 1, 0, 1f); 
            break; 
           } 
           case 2: { 
            soundPool.play(soundID_snd_2, volSND[i], volSND[i], 1, 0, 1f); 
            break; 
           } 
           case 3: { 
            soundPool.play(soundID_snd_3, volSND[i], volSND[i], 1, 0, 1f);           
            break; 
           } 
           case 4: { 
            soundPool.play(soundID_snd_4, volSND[i], volSND[i], 1, 0, 1f); 
            break; 
           } 
           case 5: { 
            soundPool.play(soundID_snd_5, volSND[i], volSND[i], 1, 0, 1f); 
            break; 
           } 
           case 6: { 
            soundPool.play(soundID_snd_6, volSND[i], volSND[i], 1, 0, 1f); 
            break; 
           } 
           case 7: { 
            soundPool.play(soundID_snd_7, volSND[i], volSND[i], 1, 0, 1f); 
            break; 
           } 
           case 8: { 
            soundPool.play(soundID_snd_8, volSND[i], volSND[i], 1, 0, 1f); 
            break; 
           } 
          } 
         } 

         try { 
          Thread.sleep(100); 
         } catch (InterruptedException e) { 
          e.printStackTrace(); 
         } 
        } 
       } 
      } 
     }); 

     // KICK-SEQ 
     if(seq_status) { 
      btn_seq_play.setBackgroundResource(R.drawable.play); 
      seq_status = false; 
      seq_thread.interrupt(); 
     } else { 
      btn_seq_play.setBackgroundResource(R.drawable.stop); 
      seq_status = true; 
      seq_thread.start(); 
     } 
    } 
}; 

回答

0

取決於長度的audiofile。 長的音樂文件不是很大,您可以保存在內存中適合媒體播放器。 短暫的聲音0.02-5秒:soundprool

而且還有流媒體文件的Audiotrack,但實施起來有點複雜。

嘗試......或者我們展示的代碼的關鍵部分也許你meesed向上或讓喜歡從主線程或某事像那

編輯內做來電:我的代碼的建議:

private View.OnClickListener seqPlay = new View.OnClickListener() { 
public void onClick(View v) { 
    btn_seq_play = (ImageButton)findViewById(R.id.btn_seq_play); 
    if(!seq_Status) 
    { 
     Thread seq_thread = new Thread(new Runnable() { 
     @Override 
     public void run() { 
      // Do loop while seq_status==true 
      ... 
     } 

     btn_seq_play.setBackgroundResource(R.drawable.stop); 
     seq_Thread.start(); 
     } 
    else 
     btn_seq_play.setBackgroundResource(R.drawable.play); 


    seq_Status=!seq_Status; 

    } 
} 

現在你的線程將與下一次他得到了while循環,並檢查seq_Status已更改爲假的開始100ms的最大反應死...,我認爲其確定要做到這一點

你可以做一個,如果他睡覺之前檢查她的eq_Status已經改變,趕在它睡覺之前,當然

+0

感謝您的快速答案..我編輯上面的線程;) – GeneralError

+0

你好,從柏林; )你能否提供你的隔音的初始化。你告訴他使用多少最大流量? –

+0

嘿Ilja :) ..我用初始化的時間點了上面的線程..總共有32個樣本可以設置爲4個音序器軌道(每個8個樣本) – GeneralError