Exercise : 17
Create an application that will play a media
file from the memory card.
Screen Shot:
XML :
main.xml :
JAVA :
main.xml :
JAVA :
SeventeenActivity.java :
package kmn.Seventeen;
import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class SeventeenActivity extends Activity
{
/** @author Y@@D */
MediaPlayer player=null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button start=(Button)findViewById(R.id.button1);
Button stop=(Button)findViewById(R.id.button2);
getSystemService(Context.AUDIO_SERVICE);
start.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0)
{
try
{
player=null;
player=new MediaPlayer();
String audioFilePath="/sdcard/braincandy.mp3";
player.setDataSource(audioFilePath);
player.prepare();
player.start();
}
catch(Exception e)
{
Toast.makeText(SeventeenActivity.this,""+e,Toast.LENGTH_LONG).show();
}
}
});
stop.setOnClickListener(new View.OnClickListener()
{
public void onClick(View arg0)
{
player.stop();
}
});
}
}
~>step to add a mp3 file to sdcard
1)goto the ddms
2)click on push button
3)and select the given mp3 file.
Enjoy.................
