[問題] Snoudpool+TouchEvent一個疑問
開發層: (應用/框架/庫/核心)
應用
問題:
我用TouchEvent的Dowm&Move來操作Soundpool
讓他指到特定的位置後播放音效
可是他會變成連續播放好幾次
有什麼辦法能讓他點一下or滑動只發出一次音效嗎??
我有自己寫了一個sleep可是他似乎沒效果-.-
感謝解答!!
程式碼: (請多利用置底文網站)
case MotionEvent.ACTION_MOVE:
if(x>=15&&x<=40){sp[5].play(m[5], 1 , 1 , 0 , 0 , 1 );delay(100);}
break;
這是sleep的程式碼~
public void delay(final int t){
new Thread()
{
public void run()
{
try
{
sleep(t);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}.start(); /* 開始執行執行緒 */
}
--
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 122.127.178.123
推
04/16 16:23, , 1F
04/16 16:23, 1F
推
04/16 16:27, , 2F
04/16 16:27, 2F
→
04/16 16:27, , 3F
04/16 16:27, 3F
手機,我也是覺得只要move到,他就一次送出好多個事件
而我之前是用MediaPlayer不會出現這種問題
但因為我一口氣需要6個所以很延遲就改用soundpool
※ 編輯: snowoffish 來自: 122.127.178.123 (04/16 16:35)
→
04/16 16:36, , 4F
04/16 16:36, 4F
推
04/16 16:53, , 5F
04/16 16:53, 5F
→
04/16 16:54, , 6F
04/16 16:54, 6F
public boolean onTouchEvent(MotionEvent event) {
float x=event.getX();
case MotionEvent.ACTION_MOVE:
if(x>=15&&x<=40){sp[5].play(m[5], 1 , 1 , 0 , 0 , 1 );
return false;}
break;
}
return super.onTouchEvent(event);
}
不好意思我Java沒學很深不太會用Boolean的ture&false可否請教要+在哪?
我+在裡面沒反應還是一樣
推
04/16 16:59, , 7F
04/16 16:59, 7F
→
04/16 16:59, , 8F
04/16 16:59, 8F
推
04/16 17:02, , 9F
04/16 17:02, 9F
→
04/16 17:02, , 10F
04/16 17:02, 10F
推
04/16 17:04, , 11F
04/16 17:04, 11F
→
04/16 17:13, , 12F
04/16 17:13, 12F
※ 編輯: snowoffish 來自: 122.127.178.123 (04/16 17:24)
推
04/16 21:40, , 13F
04/16 21:40, 13F
→
04/16 21:41, , 14F
04/16 21:41, 14F
→
04/16 21:41, , 15F
04/16 21:41, 15F
→
04/16 22:49, , 16F
04/16 22:49, 16F
public boolean bool;
public boolean onTouchEvent(MotionEvent event)
{
float x=event.getX();
float y=event.getY();
/* 顯示觸碰點的數量 */
// myText2.setText(""+event.getPointerCount());
/* event的Action判斷 */
switch(event.getAction())
{
case MotionEvent.ACTION_MOVE:
if(bool=true){
if(x>=15&&x<=40){sp[5].play(m[5], 1 , 1 , 0 , 0 , 1 );}
if(x>=70&&x<=95){sp[4].play(m[4], 1 , 1 , 0 , 0 , 1 );}
if(x>=113&&x<=140){sp[3].play(m[3], 1 , 1 , 0 , 0 , 1 );}
bool=false;}
break;
/* 觸碰事件結束 */
case MotionEvent.ACTION_UP:
bool=true;
break;
}
return super.onTouchEvent(event);
}
還是跟機關槍一樣連發~
※ 編輯: snowoffish 來自: 122.127.178.123 (04/16 23:32)
推
04/16 23:40, , 17F
04/16 23:40, 17F
→
04/16 23:48, , 18F
04/16 23:48, 18F
推
04/16 23:52, , 19F
04/16 23:52, 19F
→
04/16 23:52, , 20F
04/16 23:52, 20F
推
04/16 23:53, , 21F
04/16 23:53, 21F
→
04/16 23:54, , 22F
04/16 23:54, 22F
推
04/16 23:54, , 23F
04/16 23:54, 23F
推
04/16 23:56, , 24F
04/16 23:56, 24F
→
04/17 00:01, , 25F
04/17 00:01, 25F
我似乎失敗了...bool完全沒效果XD我把它改成false還是可以用move是哪招...
※ 編輯: snowoffish 來自: 122.127.178.123 (04/17 00:17)
→
04/17 00:22, , 26F
04/17 00:22, 26F
→
04/17 00:26, , 27F
04/17 00:26, 27F
→
04/17 00:26, , 28F
04/17 00:26, 28F
→
04/17 01:07, , 29F
04/17 01:07, 29F
→
04/17 01:09, , 30F
04/17 01:09, 30F
→
04/17 11:11, , 31F
04/17 11:11, 31F
AndroidDev 近期熱門文章
PTT數位生活區 即時熱門文章