How to create your own custom Piano Android App? – using SoundPool API

This video shows the steps to create your own custom Piano Androind App using the SoundPool API in Android Studio.

It uses a very simple layout of 7 buttons which it associates to the 7 notes of music from ‘a’ to ‘g’ using the soundID from the SoundPool.load() method.

In this video it also manipulates the tone of the sound by adjusting the speed/ rate of the sound played using the SoundPool method.

Please note: This App is developed in the Android 12, SDK level 31.

I hope you like this video. For any questions, suggestions or appreciation please contact us at: https://programmerworld.co/contact/ or email at: programmerworld1990@gmail.com

Music Notes used in this tutorial:

Source code:

package com.programmerworld.mypiano;

import androidx.appcompat.app.AppCompatActivity;

import android.media.SoundPool;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

private SoundPool soundPool;
private Integer integerSoundIDa;
private Integer integerSoundIDb;
private Integer integerSoundIDc;
private Integer integerSoundIDd;
private Integer integerSoundIDe;
private Integer integerSoundIDf;
private Integer integerSoundIDg;

private float floatSpeed = 1.0f;


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

SoundPool.Builder builder = new SoundPool.Builder();
soundPool = builder.build();

integerSoundIDa = soundPool.load(this, R.raw.a3, 1);
integerSoundIDb = soundPool.load(this, R.raw.b3, 1);
integerSoundIDc = soundPool.load(this, R.raw.c3, 1);
integerSoundIDd = soundPool.load(this, R.raw.d3, 1);
integerSoundIDe = soundPool.load(this, R.raw.e3, 1);
integerSoundIDf = soundPool.load(this, R.raw.f3, 1);
integerSoundIDg = soundPool.load(this, R.raw.g3, 1);

}

public void button1(View view){
soundPool.play(integerSoundIDa, 1, 1, 1, 0, floatSpeed);
}
public void button2(View view){
soundPool.play(integerSoundIDb, 1, 1, 1, 0, floatSpeed);
}
public void button3(View view){
soundPool.play(integerSoundIDc, 1, 1, 1, 0, floatSpeed);
}
public void button4(View view){
soundPool.play(integerSoundIDd, 1, 1, 1, 0, floatSpeed);
}
public void button5(View view){
soundPool.play(integerSoundIDe, 1, 1, 1, 0, floatSpeed);
}
public void button6(View view){
soundPool.play(integerSoundIDf, 1, 1, 1, 0, floatSpeed);
}
public void button7(View view){
soundPool.play(integerSoundIDg, 1, 1, 1, 0, floatSpeed);
}
}

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="59dp"
android:layout_marginTop="226dp"
android:onClick="button1"
android:text="@string/_1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="59dp"
android:onClick="button2"
android:text="@string/_2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="59dp"
android:onClick="button3"
android:text="@string/_3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button2" />

<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="59dp"
android:onClick="button4"
android:text="@string/_4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button3" />

<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="59dp"
android:onClick="button5"
android:text="@string/_5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button4" />

<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="59dp"
android:onClick="button6"
android:text="@string/_6"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button5" />

<Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="59dp"
android:onClick="button7"
android:text="@string/_7"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button6" />
</androidx.constraintlayout.widget.ConstraintLayout>

plugins {
id 'com.android.application'
}

android {
compileSdk 31

defaultConfig {
applicationId "com.programmerworld.mypiano"
minSdk 31
targetSdk 31
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {

implementation 'androidx.appcompat:appcompat:1.4.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

No changes required in Manifest file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.programmerworld.mypiano">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyPiano">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

Leave a Reply

%d bloggers like this: