2012-04-11 213 views
0

我試圖播放隨機聲音onTouch事件(目前正在工作),但聲音實際上播放兩次onTouch事件,而不是一次按預期。我假設這是由於UP和Down onTouch事件。有什麼想法嗎?播放隨機聲音onTouch

注意:我在測試時註釋掉了附加的隨機聲音文件。

package com.tmapps.wiub; 

import java.util.Random; 
import com.tmapps.wiub.SoundManager; 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.MotionEvent; 
import android.view.Window; 
import android.view.WindowManager; 

public class Soundboard extends Activity { 
    private SoundManager mSoundManager; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     // FULLSCREEN CODE 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
           WindowManager.LayoutParams.FLAG_FULLSCREEN); 

     setContentView(R.layout.main); 

     mSoundManager = new SoundManager(); 
     mSoundManager.initSounds(getBaseContext()); 
     mSoundManager.addSound(0, R.raw.sound0); 

    // COMMENTED OUT WHILE TESTING 
    // mSoundManager.addSound(1, R.raw.sound1); 
    // mSoundManager.addSound(2, R.raw.sound2); 
    // mSoundManager.addSound(3, R.raw.sound3); 
    // mSoundManager.addSound(4, R.raw.sound4); 
    // mSoundManager.addSound(5, R.raw.sound5); 
    // mSoundManager.addSound(6, R.raw.sound6); 
    // mSoundManager.addSound(7, R.raw.sound7); 
    // mSoundManager.addSound(8, R.raw.sound8); 
    // mSoundManager.addSound(9, R.raw.sound9); 

    }; 

     Random r = new Random(); 
     int x = r.nextInt(1); 

     switch (evt.getAction()) 
     { 
      case MotionEvent.ACTION_DOWN: 
      mSoundManager.playSound(x); 
      return super.onTouchEvent(evt); 

      case MotionEvent.ACTION_UP: 
      break; 
      default: 
      break; 
     } 

回答

0

那是因爲ACTION_DOWN和ACTION_UP都被解僱,落實代碼只對其中一個

public boolean onTouchEvent(MotionEvent evt) 
    { 
     switch (evt.getAction()) 
     { 
      case MotionEvent.ACTION_DOWN: 
      break; 
      case MotionEvent.ACTION_UP: 
      break; 
      default: 
      break; 
     } 
     return true; 
    } 
+0

感謝您的幫助kishu27。我按照您的建議更新了onTouch代碼,現在一切正常。 – n00bdev 2012-04-12 00:48:23

+0

不客氣的兄弟..很高興爲你解決 – kishu27 2012-04-12 02:13:35

+0

偶然你有什麼想法,我可以逐漸增加音量超過X秒使用AudioManager? – n00bdev 2012-04-12 02:40:05