Play Audio in Android


There are used Media Player class for playing audio file in android Programmatically . You can play audio file of your resource folder or play any file from Sd Card. Both method are describe below

 Playing from resources

Playing from res/raw/file.mp3
MediaPlayer player=MediaPlayer.create(PlaySound.this, R.raw.zara_se.mp3);
player.start();

If you want to stop the audio use the following
if(player!=null)
{
player.stop();
}
Pause audio
if(player!=null)
{
// player is the object of Media player
player.pause();
}

 

Playing from Sd card


Now if you want to play any audio file which are stored in Sd Card use the following
code . use proper path of your file in setDataSource("sdcard/music/zara_se.mp3")
String path="sdcard/music/zara_se.mp3";

MediaPlayer player=MediaPlayer.create(PlaySound.this, R.raw.zara_se.mp3);
player.setDataSource(path);
player.start();

Post a Comment

0 Comments