Blog purpose for android basic example for android app developer. any query please my contact

Thursday, 23 February 2023

how to create sound button in android studio

 how to create sound button in android studio


This Android Program demonstrates a Sound Button Application using Java.

Here is source code of the Program to demonstrate a Sound Button Application. The program is successfully compiled and run on a Windows system. The program output is also shown below.


Introducing MediaPlayer class to play sound via click on button.



1.MainActivity.java:


import android.media.MediaPlayer;

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

import android.view.View;

import android.widget.Button;

 

public class MainActivity extends Activity {

 

    MediaPlayer button_sound;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        Button b1 = (Button) findViewById(R.id.button1);

        Button b2 = (Button) findViewById(R.id.button2);

        Button b3 = (Button) findViewById(R.id.button3);

        Button b4 = (Button) findViewById(R.id.button4);

 

        b1.setOnClickListener(new View.OnClickListener() {

 

            @Override

            public void onClick(View v) {

                // TODO Auto-generated method stub

                button_sound = MediaPlayer.create(MainActivity.this,

                        R.raw.but_click);

                button_sound.start();

            }

        });

        b2.setOnClickListener(new View.OnClickListener() {

 

            @Override

            public void onClick(View v) {

                // TODO Auto-generated method stub

                button_sound = MediaPlayer.create(MainActivity.this,

                        R.raw.sound1);

                button_sound.start();

            }

        });

        b3.setOnClickListener(new View.OnClickListener() {

 

            @Override

            public void onClick(View v) {

                // TODO Auto-generated method stub

                button_sound = MediaPlayer.create(MainActivity.this,

                        R.raw.sound2);

                button_sound.start();

            }

        });

        b4.setOnClickListener(new View.OnClickListener() {

 

            @Override

            public void onClick(View v) {

                // TODO Auto-generated method stub

                button_sound = MediaPlayer.create(MainActivity.this,

                        R.raw.sound3);

                button_sound.start();

            }

        });

 

    }

 

    @Override

    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.

        getMenuInflater().inflate(R.menu.main, menu);

        return true;

    }

 

    @Override

    protected void onPause() {

        // TODO Auto-generated method stub

        super.onPause();

 

    }

 

}



2.activity_main.xml:


<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MainActivity" 

    android:background="@drawable/background">

 

    <Button

        android:id="@+id/button1"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_alignParentLeft="true"

        android:layout_alignParentTop="true"

        android:layout_marginTop="58dp"

        android:text="Sound1" />

 

    <Button

        android:id="@+id/button2"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_alignLeft="@+id/button1"

        android:layout_below="@+id/button1"

        android:layout_marginTop="59dp"

        android:text="Sound2" />

 

    <Button

        android:id="@+id/button4"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_alignLeft="@+id/button3"

        android:layout_below="@+id/button3"

        android:layout_marginTop="36dp"

        android:text="Sound4" />

 

    <Button

        android:id="@+id/button3"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_alignLeft="@+id/button2"

        android:layout_below="@+id/button2"

        android:layout_marginTop="40dp"

        android:text="Sound3" />

 

</RelativeLayout>


No comments:

Post a Comment