Showing posts with label Playing Audio. Show all posts
Showing posts with label Playing Audio. Show all posts

Wednesday, May 21, 2014

Playing audio example code in an Android application

It is really simple to use audio files in your application. Just like the graphics you use on the display to convey some information to the user, you can use audio to convey information to the user in your application.Android supports music and sound through MediaPlayer class. This class is available in package.

setVolumeControlStream() method is used to tell the Android that the user presses the volume up or down keys while this application is running. This will adjust the volume of the audio file being played instead of the ringer volume.

We need to copy the audio files to /res/raw directory.
Copying a file into the res directory causes the Android eclipse IDE to define a Java identification for you in the class. So we can simply access the audio file using R.raw.filename

Once you are done with this, all you need to do is create a MediaPlayer instance and call start() method.

MediaPlayer mp=MediaPlayer.create(this, resId);