Android MediaPlayer Play Audio Tutorial

Android have many way to control playback of audio and video in the mobile application. In the Android SDK are come with MediaPlayer which allow the developer to play audio and video. Android MediaPlayer is a high level framework to let the developer easy to learn and use is a quick manner which does not require to understand how the mediaplayer work. For the mediaplayer drawbacks, it does not able to let the developer to customize the behaviour of the mediaplayer which is not flexible for us.  In this tutorial, I will teach you on how to use android mediaplayer play audio in the android application.

Creating a New Project

1. Open Android Studio IDE in your computer.
2. Create a new project and Edit the Application name to “AudioPlayerExample”.
(Optional) You can edit the company domain or select the suitable location for current project tutorial. Then click next button to proceed.
3. Select Minimum SDK (API 15:Android 4.0.3 (IceCreamSandwich). I choose the API 15 because many android devices currently are support more than API 15. Click Next button.
4. Choose “Empty Activity” and Click Next button
5. Lastly, press finish button.

Edit activity_main.xml

After create new project, add four buttons which are play, pause , stop and restart button so we can use it to perform the media player function.

Edit class

In this class will implement the play, stop,restart and pause of the audio. There have 3 option to play the audio that you can see the following source code. In this class i load url to play the audio, while another two method is write in the comment line.

OnErrorListner to get the error occur in the mediaplayer.

OnPreaparedListener is perform when the media player is fully buffering the selected media.

OnCompletionListener is perform when it is finish to play the media.

Run your project

Finally, you can run your project in your mobile device so you can listen the voice in your area.

(Android MediaPlayer Play Audio)

Source Code

(Visited 503 times, 1 visits today)

Yong Loon Ng

Ng Yong Loon, better known as Kristofer is a software engineer and computer scientist who doubles up as an entrepreneur.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *