Android Soundpool - Touch events -


i'm making drumpad app 8 imagebuttons play sounds on button press. have been trying figure out how make press of button more responsive problem soundpool plays file when button released.

i found out there way make button react touch using ontouchevent, have no idea how implement code. have tried using both .ogg , .wav filetypes sounds, that's not issue. i'm planning create buttons loop soundfile when key held down, major issue how make buttons response touch, not release. code:

package com.example.user.soundboard; import android.os.bundle; import android.view.view; import android.view.motionevent; import android.media.audiomanager; import android.media.soundpool; import android.support.v7.app.appcompatactivity;   public class mainactivity extends appcompatactivity {   soundpool mysound, mysound2, mysound3, mysound4, mysound5, mysound6, mysound7, mysound8;  int hihatid, kickid, snareid, clapid, snare2id, hihat2id, kick2id, openhatid;   @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);     mysound = new soundpool(1, audiomanager.stream_music, 0);     mysound2 = new soundpool(1, audiomanager.stream_music, 0);     mysound3 = new soundpool(1, audiomanager.stream_music, 0);     mysound4 = new soundpool(1, audiomanager.stream_music, 0);     mysound5 = new soundpool(1, audiomanager.stream_music, 0);     mysound6 = new soundpool(1, audiomanager.stream_music, 0);     mysound7 = new soundpool(1, audiomanager.stream_music, 0);     mysound8 = new soundpool(1, audiomanager.stream_music, 0);      hihatid = mysound.load(this, r.raw.ogghat30, 1);     hihat2id = mysound.load(this, r.raw.ogghihat14, 1);     kickid = mysound.load(this, r.raw.oggkick, 1);     kick2id = mysound.load(this, r.raw.oggkick2, 1);     snareid = mysound.load(this, r.raw.oggsnare8, 1);     snare2id = mysound.load(this, r.raw.oggsnare8, 1);     clapid = mysound.load(this, r.raw.oggclap5, 1);     openhatid = mysound.load(this, r.raw.ogghat24, 1);  }  public void playhihat(view view) {     mysound.play(hihatid, 1, 1, 1, 0, 1); }  public void playhihat2(view view) {     mysound.play(hihat2id, 1, 1, 1, 0, 1); }  public void playopenhat(view view) {     mysound.play(openhatid, 1, 1, 1, 0, 1); }  public void playkick(view view) {     mysound.play(kickid, 1, 1, 1, 0, 1); }  public void playkick2(view view) {     mysound.play(kick2id, 1, 1, 1, 0, 1); }  public void playsnare(view view) {     mysound.play(snareid, 1, 1, 1, 0, 1); }  public void playsnare2(view view) {     mysound.play(snare2id, 1, 1, 1, 0, 1); }  public void playclap(view view) {     mysound.play(clapid, 1, 1, 1, 0, 1); } 

for imagebuttons i'm using onclick method in layout.

thanks help!

you can set ontouchevenlistener view can play sounds reacting touch events.

here have example:

yourbuttonview.setontouchlistener(new view.ontouchlistener() {         @override         public boolean ontouch(view view, motionevent motionevent) {             switch (motionevent.getaction()){                 case motionevent.action_down:                     //fired when touch button                     break;                  case motionevent.action_move:                     //fired once touching button , move finger without release button                     break;                  case motionevent.action_up:                     //fired when stop touching button                     break;             }             return true;         }     }); 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -