2012-01-04 36 views
0

我一直在開發簡單的鼓墊我的電話,當它涉及到一個多點觸摸事件期間,在一次打兩個聲音(即當用戶按下兩個或兩個以上的鼓聲已經運行到一個塊與此同時)。併發出聲Android設備

播放聲音我一直使用的Soundpool類。我有一個HashMap,它將一個Point散列成一個聲音。所以我的想法是,當一個點被按下時,其相應的聲音將被播放。

我有一種感覺,一次播放兩個聲音涉及分叉或創建新線程,但它沒有按我嘗試過的方式工作,它只是在每個「for each」循環中創建一個新線程點按下。

在此先感謝! -Aneem

編輯:

SoundPool database = new SoundPool(6,AudioManager.STREAM_MUSIC,100); 



protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 

    width = MeasureSpec.getSize(widthMeasureSpec); //switched for convenience 
    height = MeasureSpec.getSize(heightMeasureSpec); 
    screenFactor = (int)((height*width)/6); 

    //have to add all points to the hash map 
    widthIncrement = (int)(width/3); 
    heightIncrement = (int)(height/2); 

    pointMap.put(new Point(0,0), database.load(getContext(),R.raw.clv,1)); 
    pointMap.put(new Point(widthIncrement,0), database.load(getContext(),R.raw.pedalhh,1)); 
    pointMap.put(new Point((widthIncrement*2),0), database.load(getContext(),R.raw.shake,1)); 
    pointMap.put(new Point(0,heightIncrement), database.load(getContext(),R.raw.analoghat,1)); 
    pointMap.put(new Point(widthIncrement,heightIncrement), database.load(getContext(),R.raw.kick,1)); 
    pointMap.put(new Point((widthIncrement*2),heightIncrement), database.load(getContext(),R.raw.snare,1)); 

} 

公共布爾的onTouchEvent(MotionEvent EV){

if(ev.getAction()==ev.ACTION_DOWN){ 
     AudioManager mgr = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE); 
     int streamVolume = mgr.getStreamVolume(AudioManager.STREAM_MUSIC); 
     PointF[] touchPoints = new PointF[ev.getPointerCount()]; 
     for(int i = 0; i < ev.getPointerCount(); i++){ 
      if(ev.getPointerId(i)==ev.ACTION_DOWN){ 
       touchPoints[i] = new PointF(ev.getX(i),ev.getY(i)); 
      } 
     } 
     int i = 1; 
     for(final PointF point : touchPoints){ 
      new Thread(new Runnable(){ 
       public void run(){      
        forkSound(point);  
       } 
      }).start(); 
     } 
     return true; 
    } 
    return true; 
} 

回答

2

的Soundpool能夠同時播放多個事情。您在設置SoundPool時傳入流的數量。

郵政所有代碼,創建和設置您的Soundpool,我們可以看到,爲什麼它不工作你想怎麼交易。

SoundPool Constructor

第一個參數爲int maxStreams。無論您爲此參數傳遞將是你的Soundpool多少可以起到流一次

+0

所有這些編輯真的很抱歉,這是我的第一篇文章。 – Aneem 2012-01-04 22:36:16

+0

沒問題。它看起來像你正在創造6流,所以它應該能夠一次播放6聲音。你可以在你打電話的地方發佈代碼嗎?當你試圖一次玩多個時,它又會做什麼?它是按順序播放還是僅播放其中的一個? – FoamyGuy 2012-01-04 22:53:54

+0

我一直在搞這個應用程序,我認爲這個問題其實並不是SoundPool,而是我一直在處理多點觸控。所以基本上當我嘗試玩超過一個時,只有其中一個玩。我會發布我的代碼來處理觸摸事件。 – Aneem 2012-01-04 23:05:09