How to play Audio in android ?

There are three ways to play audio in android.

1.Create instance for Media player and setDataSource.

MediaPlayer player=new MediaPlayer();
player.setDataSource(String Path);
player.prepare(); //This is necessary before starting media player.
player.start();

2.Directly Map path to media player;

MediaPlayer mp=MediaPlayer.Create(this,R.raw.song);//song.mp3 in raw folder.

mp.start();   //Here No need to prepare the Media player,because of it is already  prepared by above code.

3.By setting Uri to setDataSource Method.


MediaPlayer player=new MediaPlayer();

player.setDataSource(this, Uri.parse(path));

player.prepareAsync();//When we have to play streams .
//Song will be started after completion of preparing...
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

				@Override
				public void onPrepared(MediaPlayer mp) {
					// TODO Auto-generated method stub
					mp.start();
				}
			});

I hope to useful for beginners ….

Advertisements

One thought on “How to play Audio in android ?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s