Audio.xmlfile
<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/o">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="17dp"
android:text="START _AUDIO _DEMO"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/stop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/Pause"
android:layout_below="@+id/Pause"
android:layout_marginTop="36dp"
android:text="STOP_MP3SONG" />
<Button
android:id="@+id/start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="44dp"
android:text="START_MP3_SONG" />
<Button
android:id="@+id/Pause"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/start"
android:layout_below="@+id/start"
android:layout_marginTop="39dp"
android:text="PAUSE_SONG" />
</RelativeLayout>
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/o">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="17dp"
android:text="START _AUDIO _DEMO"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/stop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/Pause"
android:layout_below="@+id/Pause"
android:layout_marginTop="36dp"
android:text="STOP_MP3SONG" />
<Button
android:id="@+id/start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="44dp"
android:text="START_MP3_SONG" />
<Button
android:id="@+id/Pause"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/start"
android:layout_below="@+id/start"
android:layout_marginTop="39dp"
android:text="PAUSE_SONG" />
</RelativeLayout>
Audio.javaFie
package com.example.te;
import java.io.IOException;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
int flag=0;
Button start,stop,pause;
MediaPlayer mp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start=(Button)findViewById(R.id.start);
stop=(Button)findViewById(R.id.stop);
pause=(Button)findViewById(R.id.Pause);
mp=new MediaPlayer();
try {
mp.setDataSource("sdcard/music/Tum Mile-Rock.mp3");
mp.prepare();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mp.start();
flag=1;
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag==0)
{
Toast.makeText(getApplicationContext(), "Please First StartThe Song", Toast.LENGTH_SHORT).show();
}
mp.stop();
}
});
pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag==0)
{
Toast.makeText(getApplicationContext(), "Please First StartThe Song", Toast.LENGTH_SHORT).show();
}
mp.pause();
}
});
}
@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;
}
}
import java.io.IOException;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
int flag=0;
Button start,stop,pause;
MediaPlayer mp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start=(Button)findViewById(R.id.start);
stop=(Button)findViewById(R.id.stop);
pause=(Button)findViewById(R.id.Pause);
mp=new MediaPlayer();
try {
mp.setDataSource("sdcard/music/Tum Mile-Rock.mp3");
mp.prepare();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mp.start();
flag=1;
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag==0)
{
Toast.makeText(getApplicationContext(), "Please First StartThe Song", Toast.LENGTH_SHORT).show();
}
mp.stop();
}
});
pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag==0)
{
Toast.makeText(getApplicationContext(), "Please First StartThe Song", Toast.LENGTH_SHORT).show();
}
mp.pause();
}
});
}
@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;
}
}
OUTPUT