Вопрос

I have project to make Android Application music Instrument, the Application work like a Piano, if I click ImageButton with SingleTouch his play sound but I want application work with MultiTouch, I don't know to implement in my code ?

Following is my code,

private int mSoundID;
private int mSoundID2;
private int mSoundID3;
private int mSoundID4;
private int mSoundID5;
private int mSoundID6;
private int mSoundID7;
private int mSoundID8;
private int mSoundID9;
private int mSoundID10;
private int mSoundID11;
private int mSoundID12;
private int mSoundID13;
private int mSoundID14;
private int mSoundID15;
private int mSoundID16;
private boolean mSoundLoaded = true;
private float mVolume = 0f;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.talempong);

    View view = findViewById(R.id.imagebutton1);
    view.setOnTouchListener(this);

    View view2 = findViewById(R.id.imagebutton2);
    view2.setOnTouchListener(this);

    View view3 = findViewById(R.id.imagebutton3);
    view3.setOnTouchListener(this);

    View view4 = findViewById(R.id.imagebutton4);
    view4.setOnTouchListener(this);

    View view5 = findViewById(R.id.imagebutton5);
    view5.setOnTouchListener(this);

    View view6 = findViewById(R.id.imagebutton6);
    view6.setOnTouchListener(this);

    View view7 = findViewById(R.id.imagebutton7);
    view7.setOnTouchListener(this);

    View view8 = findViewById(R.id.imagebutton8);
    view8.setOnTouchListener(this);

    View view9 = findViewById(R.id.imagebutton9);
    view9.setOnTouchListener(this);

    View view10 = findViewById(R.id.imagebutton10);
    view10.setOnTouchListener(this);

    View view11 = findViewById(R.id.imagebutton11);
    view11.setOnTouchListener(this);

    View view12 = findViewById(R.id.imagebutton12);
    view12.setOnTouchListener(this);

    View view13 = findViewById(R.id.imagebutton13);
    view13.setOnTouchListener(this);

    View view14 = findViewById(R.id.imagebutton14);
    view14.setOnTouchListener(this);

    View view15 = findViewById(R.id.imagebutton15);
    view15.setOnTouchListener(this);

    View view16 = findViewById(R.id.imagebutton16);
    view16.setOnTouchListener(this);

    // Set the hardware buttons to control the music
    this.setVolumeControlStream(AudioManager.STREAM_MUSIC);

    // Load the sound
    mSoundPool = new SoundPool(16, AudioManager.STREAM_MUSIC, 0);
    mSoundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
        @Override
        public void onLoadComplete(SoundPool soundPool, int sampleId, int status) 
        {
             {
                mSoundLoaded = true;
            }
        }
    });

    mSoundID = mSoundPool.load(this, R.raw.doo, 1);
    mSoundID2 = mSoundPool.load(this, R.raw.re, 1);
    mSoundID3 = mSoundPool.load(this, R.raw.mi, 1);
    mSoundID4 = mSoundPool.load(this, R.raw.fa, 1);
    mSoundID5 = mSoundPool.load(this, R.raw.sol, 1);
    mSoundID6 = mSoundPool.load(this, R.raw.la, 1);
    mSoundID7 = mSoundPool.load(this, R.raw.si, 1);
    mSoundID8 = mSoundPool.load(this, R.raw.do_tinggi, 1);
    mSoundID9 = mSoundPool.load(this, R.raw.sol_rendah, 1);
    mSoundID10 = mSoundPool.load(this, R.raw.la_rendah, 1);
    mSoundID11 = mSoundPool.load(this, R.raw.si_rendah, 1);
    mSoundID12 = mSoundPool.load(this, R.raw.re_kres, 1);
    mSoundID13 = mSoundPool.load(this, R.raw.fa_kres, 1);
    mSoundID14 = mSoundPool.load(this, R.raw.sol_kres, 1);
    mSoundID15 = mSoundPool.load(this, R.raw.la_kres, 1);
    mSoundID16 = mSoundPool.load(this, R.raw.re_tinggi, 1);

    // Getting the user sound settings
    AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
    float actualVolume = (float) audioManager
            .getStreamVolume(AudioManager.STREAM_MUSIC);
    float maxVolume = (float) audioManager
            .getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    mVolume = actualVolume / maxVolume;
}
//single touch
public boolean onTouch(View v, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN ){
//multitouch

        // Is the sound loaded already?
        if (mSoundLoaded) {
            if (v.getId() == R.id.imagebutton1) {
                playSound(mSoundID);
                Log.e("Test", "Played mSoundID");

            } else if (v.getId() == R.id.imagebutton2) {
                playSound(mSoundID2);
                Log.e("Test", "Played mSoundID2");
            } else if (v.getId() == R.id.imagebutton3) {
                playSound(mSoundID3);
                Log.e("Test", "Played mSoundID3");
            } else if (v.getId() == R.id.imagebutton4) {
                playSound(mSoundID4);
                Log.e("Test", "Played mSoundID4");
            } else if (v.getId() == R.id.imagebutton5) {
                playSound(mSoundID5);
                Log.e("Test", "Played mSoundID5");
            } else if (v.getId() == R.id.imagebutton6) {
                playSound(mSoundID6);
                Log.e("Test", "Played mSoundID6");
            } else if (v.getId() == R.id.imagebutton7) {
                playSound(mSoundID7);
                Log.e("Test", "Played mSoundID7");
            } else if (v.getId() == R.id.imagebutton8) {
                playSound(mSoundID8);
                Log.e("Test", "Played mSoundID8");
            } else if (v.getId() == R.id.imagebutton9) {
                playSound(mSoundID9);
                Log.e("Test", "Played mSoundID9");
            } else if (v.getId() == R.id.imagebutton10) {
                playSound(mSoundID10);
                Log.e("Test", "Played mSoundID10");
            } else if (v.getId() == R.id.imagebutton11) {
                playSound(mSoundID11);
                Log.e("Test", "Played mSoundID11");
            } else if (v.getId() == R.id.imagebutton12) {
                playSound(mSoundID12);
                Log.e("Test", "Played mSoundID12");
            } else if (v.getId() == R.id.imagebutton13) {
                playSound(mSoundID13);
                Log.e("Test", "Played mSoundID13");
            } else if (v.getId() == R.id.imagebutton14) {
                playSound(mSoundID14);
                Log.e("Test", "Played mSoundID14");
            } else if (v.getId() == R.id.imagebutton15) {
                playSound(mSoundID15);
                Log.e("Test", "Played mSoundID15");
            } else if (v.getId() == R.id.imagebutton16) {
                playSound(mSoundID16);
                Log.e("Test", "Played mSoundID16");

            }
        }
    }
    return false;
}

private void playSound(int soundId) {
    mSoundPool.play(soundId, mVolume, mVolume, 1, 0, 1f);
}


}
Это было полезно?

Решение

Ok , I make some thing like that , i force to make multi-touch on device which doesn't support multi-touch to my implementation was i create array that will hold the keys that you are clicked and loop on each of them in array and make the sound and don't to forget empty the array in the last

 View view = findViewById(R.id.imagebutton1);
        view.setOnTouchListener(this);
       view.setTag(1);
        View view2 = findViewById(R.id.imagebutton2);
        view2.setOnTouchListener(this);
        view2.setTag(2);

and so on for each view of you add tag

and make globe array

  boolean touchedViews [] = new boolean [viewsNumber];

and in touch event make the code below

 public boolean onTouch(View v, MotionEvent event) {
                    int action = event.getAction()

            synchronized (this) {                   

       switch (action) {
             case MotionEvent.ACTION_DOWN:// when you touch down

                 touchedViews[ (Integer) v.getTag()] = true;

                 break ;
             case case MotionEvent.ACTION_UP: // when you touch up
                 break;
             }

            }

            for(int i=0 ; i < touchedViews.length ; i++){
                // play sound of each view with true touch 
            }
        }

Другие советы

You should read Handling Multi-Touch Gestures

Android work multitouch button also seems as if it solves the same issue.

Note : some variables will not be declared in my code but its name very obvious for you to add their declaration

first of all you will implement custom view that will hold piano keyboard and add all keys on your own style and Load your sounds and declare the Paints in

**  protected void onLayout(boolean changed, int left, int top, int right, int bottom)**

and draw your keys with paints in ondraw method

 protected void onDraw(Canvas canvas) 

and here the implementation of the Ontouch event and it support mutli-touch keys

    @Override
public boolean onTouchEvent(MotionEvent event) {
    int pointerCount = event.getPointerCount();
    int cappedPointerCount = pointerCount > MAX_FINGERS ? MAX_FINGERS : pointerCount;
    int actionIndex = event.getActionIndex();
    int action = event.getActionMasked();
    int id = event.getPointerId(actionIndex);

    if ((action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_POINTER_DOWN) && id < MAX_FINGERS) {
        mFingerPoints[id] = new Point((int) event.getX(actionIndex), (int) event.getY(actionIndex));
    } else if ((action == MotionEvent.ACTION_POINTER_UP || action == MotionEvent.ACTION_UP) && id < MAX_FINGERS) {
        mFingerPoints[id] = null;
        invalidateKey(mFingerTones[id]);
        mFingerTones[id] = -1;
    }

    for (int i = 0; i < cappedPointerCount; i++) {
        int index = event.findPointerIndex(i);
        if (mFingerPoints[i] != null && index != -1) {
            mFingerPoints[i].set((int) event.getX(index), (int) event.getY(index));
            int tone = getToneForPoint(mFingerPoints[i]);
            if (tone != mFingerTones[i] && tone != -1) {
                invalidateKey(mFingerTones[i]);
                mFingerTones[i] = tone;
                invalidateKey(mFingerTones[i]);
                if (!isKeyDown(i)) {
                    int poolIndex = mToneToIndexMap.get(mFingerTones[i]);
                    event.getPointerCoords(index, mPointerCoords);
                    float volume = mPointerCoords.getAxisValue(MotionEvent.AXIS_PRESSURE);
                    volume = volume > 1f ? 1f : volume;
                    mSoundPool.play(poolIndex, volume, volume, 0, 0, 1f);
                }
            }
        }
    }



    return true;
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top