Please make a project with the name AudioDemo and make a main class with the name AudioDemo.java and have the following code
package com.jitesh.audiodemo;
import java.io.IOException;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
public class AudioDemo extends Activity implements
MediaPlayer.OnCompletionListener {
private static ProgressDialog progressDialog;
private ImageButton play;
private ImageButton pause;
private ImageButton stop;
private ImageButton replay;
private MediaPlayer mp;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
play = (ImageButton) findViewById(R.id.play);
pause = (ImageButton) findViewById(R.id.pause);
stop = (ImageButton) findViewById(R.id.stop);
replay = (ImageButton) findViewById(R.id.replay);
play.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
play();
}
});
pause.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
pause();
}
});
stop.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
stop();
}
});
replay.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
setup();
}
});
setup();
}
@Override
public void onDestroy() {
super.onDestroy();
if (stop.isEnabled()) {
stop();
}
}
public void onCompletion(MediaPlayer mp) {
stop();
}
private void play() {
Log.d("play", "reached");
mp.start();
play.setEnabled(false);
pause.setEnabled(true);
stop.setEnabled(true);
}
private void stop() {
Log.d("stop", "reached");
mp.stop();
pause.setEnabled(false);
stop.setEnabled(false);
try {
setup();
} catch (Throwable t) {
goBlooey(t);
}
}
private void pause() {
Log.d("pause", "reached");
mp.pause();
play.setEnabled(true);
pause.setEnabled(false);
stop.setEnabled(true);
}
private void loadClip() {
try {
mp = MediaPlayer.create(this, R.raw.clip);
mp.setOnCompletionListener(this);
} catch (Throwable t) {
goBlooey(t);
}
}
private void setup() {
try {
progressDialog = ProgressDialog.show(AudioDemo.this, "",
"Buffering audio...", true);
progressDialog.setCancelable(true);
mp = new MediaPlayer();
mp.setDataSource("http://www.virginmegastore.me/Library/Music/CD_001214/Tracks/Track1.mp3");
mp.prepareAsync();
mp.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
Log.d("first", "reached");
// mp.start();
progressDialog.dismiss();
}
});
} 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();
}
play.setEnabled(true);
pause.setEnabled(false);
stop.setEnabled(false);
}
private void goBlooey(Throwable t) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Exception!").setMessage(t.toString())
.setPositiveButton("OK", null).show();
}
}
the main.xml shoupd look like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="4dip" >
<ImageButton
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:paddingRight="4dip"
android:src="@drawable/play" />
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:text="Play"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="4dip" >
<ImageButton
android:id="@+id/pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="4dip"
android:src="@drawable/pause" />
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:text="Pause"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="4dip" >
<ImageButton
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="4dip"
android:src="@drawable/stop" />
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:text="Stop"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="4dip" >
<ImageButton
android:id="@+id/replay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="4dip"
android:src="@drawable/restart" />
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:text="Replay"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
</LinearLayout>
do not foget to add the permission at manidest
<uses-permission android:name="android.permission.INTERNET" />
the screen shots are attached as follows
Yo can download the audio from a link AudioDemo
package com.jitesh.audiodemo;
import java.io.IOException;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
public class AudioDemo extends Activity implements
MediaPlayer.OnCompletionListener {
private static ProgressDialog progressDialog;
private ImageButton play;
private ImageButton pause;
private ImageButton stop;
private ImageButton replay;
private MediaPlayer mp;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
play = (ImageButton) findViewById(R.id.play);
pause = (ImageButton) findViewById(R.id.pause);
stop = (ImageButton) findViewById(R.id.stop);
replay = (ImageButton) findViewById(R.id.replay);
play.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
play();
}
});
pause.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
pause();
}
});
stop.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
stop();
}
});
replay.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
setup();
}
});
setup();
}
@Override
public void onDestroy() {
super.onDestroy();
if (stop.isEnabled()) {
stop();
}
}
public void onCompletion(MediaPlayer mp) {
stop();
}
private void play() {
Log.d("play", "reached");
mp.start();
play.setEnabled(false);
pause.setEnabled(true);
stop.setEnabled(true);
}
private void stop() {
Log.d("stop", "reached");
mp.stop();
pause.setEnabled(false);
stop.setEnabled(false);
try {
setup();
} catch (Throwable t) {
goBlooey(t);
}
}
private void pause() {
Log.d("pause", "reached");
mp.pause();
play.setEnabled(true);
pause.setEnabled(false);
stop.setEnabled(true);
}
private void loadClip() {
try {
mp = MediaPlayer.create(this, R.raw.clip);
mp.setOnCompletionListener(this);
} catch (Throwable t) {
goBlooey(t);
}
}
private void setup() {
try {
progressDialog = ProgressDialog.show(AudioDemo.this, "",
"Buffering audio...", true);
progressDialog.setCancelable(true);
mp = new MediaPlayer();
mp.setDataSource("http://www.virginmegastore.me/Library/Music/CD_001214/Tracks/Track1.mp3");
mp.prepareAsync();
mp.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
Log.d("first", "reached");
// mp.start();
progressDialog.dismiss();
}
});
} 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();
}
play.setEnabled(true);
pause.setEnabled(false);
stop.setEnabled(false);
}
private void goBlooey(Throwable t) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Exception!").setMessage(t.toString())
.setPositiveButton("OK", null).show();
}
}
the main.xml shoupd look like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="4dip" >
<ImageButton
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:paddingRight="4dip"
android:src="@drawable/play" />
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:text="Play"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="4dip" >
<ImageButton
android:id="@+id/pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="4dip"
android:src="@drawable/pause" />
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:text="Pause"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="4dip" >
<ImageButton
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="4dip"
android:src="@drawable/stop" />
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:text="Stop"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="4dip" >
<ImageButton
android:id="@+id/replay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="4dip"
android:src="@drawable/restart" />
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:text="Replay"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
</LinearLayout>
do not foget to add the permission at manidest
<uses-permission android:name="android.permission.INTERNET" />
the screen shots are attached as follows
the used resources are give below as drawables.
Yo can download the audio from a link AudioDemo
Thank you for this!
ReplyDeleteYour code worked for me first time.
Your tutorial is much appreciated.
mp = MediaPlayer.create(this, R.raw.clip);
ReplyDeletethis line is giving error R.raw is unidentified
please help
please download the fulll code
DeleteHi, I downloaded full code and running it from Android studio, it says Module not found.
ReplyDeleteAlso Do you have any code for online audio stream.
Please help.
i want to get list of audios in listview/recyclerview from any url..what should i do for this???
ReplyDelete