• Have Any Question?
  • contact@eitworld.com

write an android application to play sound and stop sound on button click.





Here we have created an application when we click on the play sound button then the sound will be started and if we will click on the stop sound button then the sound will be stopped. The xml file is as follows:

<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” ><TextView
android:id=”@+id/textView1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Click play to listen and click stop to end sound” />

<Button
android:id=”@+id/btnPlay”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentLeft=”true”
android:layout_below=”@+id/textView1″
android:layout_marginTop=”134dp”
android:text=”Play Sound” />

<Button
android:id=”@+id/btnStop”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignBaseline=”@+id/btnPlay”
android:layout_alignBottom=”@+id/btnPlay”
android:layout_marginLeft=”46dp”
android:layout_toRightOf=”@+id/btnPlay”
android:text=”Stop Sound” />

</RelativeLayout>

In this layout file, we have used the relative layout. We have used a text view that show a message on the activity such as “click play to listen and click stop to end sound”, here we have used two buttons on which we have to perform start and stop function for the sound.
Here is the java file as follows:

package com.example.androidmultimedia;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;public class MainActivity extends Activity {
Button play,stop;
MediaPlayer mp1;

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

play = (Button) findViewById(R.id.btnPlay);
stop = (Button) findViewById(R.id.btnStop);

play.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
playSound(arg0);
}
});

stop.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
stopSound(arg0);
}
});
}
public void playSound(View view)
{
mp1 = MediaPlayer.create(this, R.raw.ans1);
mp1.start();
}
public void stopSound(View view)
{
mp1.stop();
}
@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;}
}

In this java file, we have used the following packages:

android

Here 1st line represent the package name of the application by which our application can be identified uniquely, and android.media.MediaPlayer provides the method related to the multimedia operations so that we can perform the play and stop operations.
After that we have a class that extends the Activity class as follows:
android
Now we have to declare the variables as follows:
android
Here we have declared play, stop variables of type Button and mp1 variable of the MediaPlayer type. Now we have to register the controls that are defined in the xml file by using their id as follows:
android
Inside the onCretate() method we have to register. Now we have to perform the action on buttons, so we have used the setOnClickListener() method. Here for play button, inside the onClick() method we have called the playSound() method as follows:
android
Inside this method, we have assigned the current context and the URL of the sound by using the static method MediaPlayer.create() to the mp1 variable of type MediaPlayer. Now we have to call the start() method by using the object of the MediaPlayer. When the application will launch then we click on this button then the sound will be play again and again when we click on this button after end of the sound.
After that we have to define the functionality for the stop button. Here inside the onClick() method. One method stopSound() is called as follows:
android
Here stop() method is used to end the sound every time when we will click on this button.
The output is as follows:

android



Real Time Web Analytics