2015-01-14 5 views
1

Я хочу сделать это, когда кнопка удерживается нажатой, она начинает воспроизводить звук, а при отпускании останавливается. Я знаю, что есть несколько людей с одинаковой проблемой, но я не нашел решения проблемы.Воспроизведение звука при нажатии и остановке при отпускании

Вот мой код:

private ImageButton pad1, pad2, pad3, pad4, pad5, pad6, pad7, pad8, pad9, pad10, pad11, pad12; 
SoundPool soundPool = null; 
int sound1_id; 
int sound2_id; 
int sound3_id; 
int sound4_id; 
int sound5_id; 
int sound6_id; 
int sound7_id; 
int sound8_id; 
int sound9_id; 
int sound10_id; 
int sound11_id; 
int sound12_id; 

pad1 = (ImageButton) findViewById(R.id.pad1); 
    pad2 = (ImageButton) findViewById(R.id.pad2); 
    pad3 = (ImageButton) findViewById(R.id.pad3); 
    pad4 = (ImageButton) findViewById(R.id.pad4); 
    pad5 = (ImageButton) findViewById(R.id.pad5); 
    pad6 = (ImageButton) findViewById(R.id.pad6); 
    pad7 = (ImageButton) findViewById(R.id.pad7); 
    pad8 = (ImageButton) findViewById(R.id.pad8); 
    pad9 = (ImageButton) findViewById(R.id.pad9); 
    pad10 = (ImageButton) findViewById(R.id.pad10); 
    pad11 = (ImageButton) findViewById(R.id.pad11); 
    pad12 = (ImageButton) findViewById(R.id.pad12); 


    soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0); 
    sound1_id = soundPool.load(this, R.raw.p1, 1); 
    sound2_id = soundPool.load(this, R.raw.p2, 1); 
    sound3_id = soundPool.load(this, R.raw.p3, 1); 
    sound4_id = soundPool.load(this, R.raw.p4, 1); 
    sound5_id = soundPool.load(this, R.raw.p5, 1); 
    sound6_id = soundPool.load(this, R.raw.p6, 1); 
    sound7_id = soundPool.load(this, R.raw.p7, 1); 
    sound8_id = soundPool.load(this, R.raw.p8, 1); 
    sound9_id = soundPool.load(this, R.raw.p9, 1); 
    sound10_id = soundPool.load(this, R.raw.p10, 1); 
    sound11_id = soundPool.load(this, R.raw.p11, 1); 
    sound12_id = soundPool.load(this, R.raw.p12, 1); 


public void onPad1Click(View view) { 
    soundPool.play(sound1_id, (float) 0.5, (float) 0.5, 1, 0, 1.f); 
} 

public void onPad2Click(View view) { 
    soundPool.play(sound2_id, (float) 0.5, (float) 0.5, 1, 0, 1.f); 
} 

public void onPad3Click(View view) { 
    soundPool.play(sound3_id, (float) 0.5, (float) 0.5, 1, 0, 1.f); 

} 

public void onPad4Click(View view) { 

    soundPool.play(sound4_id, (float) 0.5, (float) 0.5, 1, 0, 1.f); 
} 

public void onPad5Click(View view) { 
    soundPool.play(sound5_id, (float) 0.5, (float) 0.5, 1, 0, 1.f); 
} 

public void onPad6Click(View view) { 
    soundPool.play(sound6_id, (float) 0.5, (float) 0.5, 1, 0, 1.f); 
} 

public void onPad7Click(View view) { 
    soundPool.play(sound7_id, (float) 0.5, (float) 0.5, 1, 0, 1.f); 
} 

public void onPad8Click(View view) { 
    soundPool.play(sound8_id, (float) 0.5, (float) 0.5, 1, 0, 1.f); 
} 

public void onPad9Click(View view) { 
    soundPool.play(sound9_id, (float) 0.5, (float) 0.5, 1, 0, 1.f); 
} 

public void onPad10Click(View view) { 
    soundPool.play(sound10_id, (float) 0.5, (float) 0.5, 1, 0, 1.f); 
} 

public void onPad11Click(View view) { 
    soundPool.play(sound11_id, (float) 0.5, (float) 0.5, 1, 0, 1.f); 
} 

public void onPad12Click(View view) { 
    soundPool.play(sound12_id, (float) 0.5, (float) 0.5, 1, 0, 1.f); 
} 

}

+2

Это немного более утомительным, но вы можете установить пользовательские onTouchListener для каждой кнопки и обработать его там, как в этом примере [] (http://stackoverflow.com/questions/3784514/захват-кнопка-релиз-в-андроид). Или вы можете написать один из них для каждого из них, который определяет, какой звук будет воспроизводиться на основе идентификатора вида. – aProperFox

+0

Так вы сталкиваетесь с проблемой с кодом, который вы разместили? Если да, можете ли вы указать, что является проблемой? – AADProgramming

+0

никаких проблем, код отлично работает – Ivaylo

ответ

0

Посмотрите на приведенный ниже код. К сожалению, у меня не было времени проверить, работает ли оно, но прокомментируйте здесь, если у вас возникнут проблемы, я помогу отладить его.

import android.media.SoundPool; 
import android.view.MotionEvent; 
import android.view.View; 

public class LoopSoundWhilePressing implements View.OnTouchListener { 

private SoundPool soundPool; 
private int soundId; 
private int playedSoundId; //You need the id that's returned from SoundPool.play() to stop it later on 
private boolean isPlaying; 

public LoopSoundWhilePressing(SoundPool soundPool, int soundId) { 
    this.soundPool = soundPool; //You give the soundPool and soundId for each instance. 
    this.soundId = soundId; //The sound you will be playing 
} 

@Override 
public boolean onTouch(View v, MotionEvent event) { 

    boolean handled = false; 

    switch (event.getAction()) { 
     case MotionEvent.ACTION_DOWN: { 
      if(!isPlaying) { 
       //The -1 will make it loop until we stop it on ActionUp 
       playedSoundId = soundPool.play(soundId, (float) 0.5, (float) 0.5, 1, -1, 1.f); 
       isPlaying = true; 
      } 
      handled = true; 
      break; 
     } 

     case MotionEvent.ACTION_UP: { 
      if(isPlaying) { 
       soundPool.stop(playedSoundId); 
       isPlaying = false; 
      } 
      handled = true; 
      break; 
     } 
    } 

    return handled; 
} 

}

Смежные вопросы