How to add click sound to buttons

Keywords: Android less SDK Mobile

preface

Many well-made apps have their own click sound effect, so how to achieve this effect simply is a concept called SoundPool , this class is mainly used to play some smaller audio files, because it is more convenient, usually used in the game more.

code

Without much gossip, we need to do a function now, which is to play the sound effect at the same time when clicking a button.

First, prepare your audio file. Then, under your rec, name a folder of resume raw, and put in the audio file, as shown in the figure:

Then the layout file has only one button

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/btnPlay"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_margin="5dp"
        android:text="Play Wav"
        tools:ignore="HardcodedText" />

</LinearLayout>

Then MainActivity

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btnPlay;

    private SoundPool soundPool;//Declare a SoundPool
    private int soundID;//Create an audio ID for a sound

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        btnPlay = (Button) findViewById(R.id.btnPlay);
        btnPlay.setOnClickListener(this);
        initSound();
    }

    @SuppressLint("NewApi")
    private void initSound() {
        soundPool = new SoundPool.Builder().build();
        soundID = soundPool.load(this, R.raw.testsong, 1);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnPlay:
                playSound();
                break;
        }
    }

    private void playSound() {
        soundPool.play(
                soundID,
                0.1f,      //Left ear canal volume [0 ~ 1]
                0.5f,      //Right ear canal volume [0 ~ 1]
                0,         //Playback priority [0 indicates the lowest priority]
                1,         //Cycle mode [0 means cycle once, - 1 means cycle all the time, other means number + 1 means cycle times corresponding to current number]
                1          //Playback speed [1 is normal, range from 0 to 2]
        );
    }
}

In this way, when you click the button, the sound effect will be played automatically. Note that the way to initialize SoundPool here is a new way provided by Android after 5.0, which can be used before 5.0:


soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);

Among them, the construction method parameters are not explained. With the development of Android, the share before 5.0 will be less and less, so try to use the method provided by the newer SDK in the future.

After the above code is completed, run it on the mobile phone. Click to hear the sound effect.

PS: welcome to the Android Xiaobai Development Group [541144061] for irregular driving

Posted by icedude on Sat, 23 May 2020 09:28:01 -0700