Membuat DrumPad Sederhana dengan Android Studio


  • Activity_main.xml
  • <?xml version="1.0" encoding="utf-8"?>
    <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorPrimary"
        tools:context=".MainActivity"
        android:padding="10dp"
        android:gravity="center"
        android:orientation="vertical">
    
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <Button
                android:background="@drawable/blue_button"
                android:onClick="playsound1"
                android:layout_width="100dp"
                android:layout_height="100dp"/>
            <Button
                android:layout_marginLeft="5dp"
                android:background="@drawable/blue_button"
                android:onClick="playsound2"
                android:layout_width="100dp"
                android:layout_height="100dp"/>
            <Button
                android:layout_marginLeft="5dp"
                android:background="@drawable/blue_button"
                android:onClick="playsound3"
                android:layout_width="100dp"
                android:layout_height="100dp"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_marginTop="5dp">
            <Button
                android:background="@drawable/purple_button"
                android:onClick="playsound4"
                android:layout_width="100dp"
                android:layout_height="100dp"/>
            <Button
                android:layout_marginLeft="5dp"
                android:background="@drawable/purple_button"
                android:onClick="playsound5"
                android:layout_width="100dp"
                android:layout_height="100dp"/>
            <Button
                android:layout_marginLeft="5dp"
                android:background="@drawable/purple_button"
                android:onClick="playsound6"
                android:layout_width="100dp"
                android:layout_height="100dp"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_marginTop="5dp">
            <LinearLayout
                android:background="@drawable/orange_button"
                android:onClick="playsound7"
                android:layout_width="100dp"
                android:layout_height="100dp"/>
            <Button
                android:layout_marginLeft="5dp"
                android:background="@drawable/orange_button"
                android:onClick="playsound8"
                android:layout_width="100dp"
                android:layout_height="100dp"/>
            <Button
                android:layout_marginLeft="5dp"
                android:background="@drawable/orange_button"
                android:onClick="playsound9"
                android:layout_width="100dp"
                android:layout_height="100dp"/>
        </LinearLayout>
    
    
    </LinearLayout>


  • MainActivity.java
  • package com.example.teddy.drumpad;
    
    import android.media.AudioManager;
    import android.media.SoundPool;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    
    public class MainActivity extends AppCompatActivity {
        private SoundPool sp;
        private int sound1;
        private int sound2;
        private int sound3;
        private int sound4;
        private int sound5;
        private int sound6;
        private int sound7;
        private int sound8;
        private int sound9;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            sp = new SoundPool(2, AudioManager.STREAM_MUSIC,0);
            sound1 = sp.load(getApplicationContext(),R.raw.sound1,1);
            sound2 = sp.load(getApplicationContext(),R.raw.sound2,1);
            sound3 = sp.load(getApplicationContext(),R.raw.sound3,1);
            sound4 = sp.load(getApplicationContext(),R.raw.sound4,1);
            sound5 = sp.load(getApplicationContext(),R.raw.sound5,1);
            sound6 = sp.load(getApplicationContext(),R.raw.sound6,1);
            sound7 = sp.load(getApplicationContext(),R.raw.sound7,1);
            sound8 = sp.load(getApplicationContext(),R.raw.sound8,1);
            sound9 = sp.load(getApplicationContext(),R.raw.sound9,1);
    
        }
    
        public void playsound1 (View v) {
            sp.play(sound1,0.5f,0.5f,0,0,10f);
        }
        public void playsound2 (View v) {
            sp.play(sound2,1.0f,1.0f,0,0,10f);
        }
        public void playsound3 (View v) {
            sp.play(sound3,1.0f,1.0f,0,0,10f);
        }
        public void playsound4 (View v) {
            sp.play(sound4,1.0f,1.0f,0,0,10f);
        }
        public void playsound5 (View v) {
            sp.play(sound5,1.0f,1.0f,0,0,10f);
        }
        public void playsound6 (View v) {
            sp.play(sound6,1.0f,1.0f,0,0,10f);
        }
        public void playsound7 (View v) {
            sp.play(sound7,1.0f,1.0f,0,0,10f);
        }
        public void playsound8 (View v) {
            sp.play(sound8,1.0f,1.0f,0,0,10f);
        }
        public void playsound9 (View v) {
            sp.play(sound9,1.0f,1.0f,0,0,10f);
        }
    }
    


    • Tampilan


    Komen dong