Android Programming

Hands On No. 8 : Working with Audio Files

Resources

Source Code of MainAcivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button play = (Button) findViewById(R.id.btn1);
Button pause = (Button) findViewById(R.id.btn2);
Button stop1 = (Button) findViewById(R.id.btn3);
MediaPlayer mPlayer = MediaPlayer.create(MainActivity.this, R.raw.iphone);

play.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {


mPlayer.start();



}
});
pause.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {

mPlayer.pause();


}
});
stop1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {

mPlayer.stop();

}
});



}
Source Code of 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:orientation="vertical"
tools:context=".MainActivity" >

<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="PLAY" android:layout_marginTop="30dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
/>
<Button
android:id="@+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="PAUSE" android:layout_marginTop="40dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp" />
<Button
android:id="@+id/btn3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="STOP" android:layout_marginTop="40dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp" />
</LinearLayout>

Add Following permission in AndroidManifest.xml File

<uses-permission android:name="android.permission.INTERNET" />

Download The App