samdark / yiiframework-ru

yiiframework.ru
http://yiiframework.ru/
Other
52 stars 21 forks source link

Android Studio Sound Pool #149

Closed arman9828 closed 4 years ago

arman9828 commented 4 years ago

I am a noob at programming. I used Sound pool for Drum Pads, and I have a problem. The thing is, a drum pad makes the sound when I release the pad, but not when I press it. (onClick) Can someone help me with that?

And also i want to some of the pads play while pad is pressed, and the sound should stop when I release the button. Thank you for attention, have a good day. code:

package com.armooq.midimpc;

import android.media.AudioAttributes; import android.media.AudioManager; import android.media.SoundPool; import android.os.Build; import android.os.Bundle; import android.view.View; import android.view.Window; import android.view.WindowManager;

import androidx.appcompat.app.AppCompatActivity;

public class MPC extends AppCompatActivity {

private SoundPool soundPool;
private int kick, snare, clap, cl_hat, o_hat;
private int sound3StreamId;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mpc);

    Window w = getWindow();
    w.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        AudioAttributes audioAttributes = new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_MEDIA )
                .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                .build();

        soundPool = new SoundPool.Builder()
                .setMaxStreams(6)
                .setAudioAttributes(audioAttributes)
                .build();

    kick = soundPool.load(this, R.raw.kick, 1);
    snare = soundPool.load(this, R.raw.snare, 1);
    clap = soundPool.load(this, R.raw.clap, 1);
    cl_hat = soundPool.load(this, R.raw.cl_hat, 1);
    o_hat = soundPool.load(this, R.raw.o_hat, 1);
}

public void playSound(View v) {
    switch (v.getId()) {
        case R.id.redPad1:
            soundPool.play(kick, 1, 1, 0, 0, 1);
            break;
        case R.id.redPad2:
            soundPool.play(snare, 1, 1, 0, 0, 1);
            break;
        case R.id.redPad3:
            sound3StreamId = soundPool.play(clap, 1, 1, 0, 0, 1);
            break;
        case R.id.pinkPad3:
            soundPool.play(cl_hat, 1, 1, 0, 0, 1);
            break;
        case R.id.pinkPad4:
            soundPool.play(o_hat, 1, 1, 0, 0, 1);
            break;
    }

}

@Override
protected void onDestroy() {
    super.onDestroy();
    soundPool.release();
    soundPool = null;
}

}