1. Introduction :

 

In this tutorial we will deal with android playing audio file, i.e, sometimes we need to play a sound or even when we want to play audio songs just like media player this tutorial is for you.

 

We will be using a service class to do the process if you are not aware of using service class i will explain about it in coming tutorials.

Here now we will play audio file on button click.

playbut

 

 

2. Creating activity_main.xml :

 

We are tying to make a android play audio file from assets / android play audio file from raw so for this we need a simple UI so that it has a play and stop button.

So you have the full freedom in designing in implementing according to your desire, but make sure that buttons are working correctly.

 

 

3. Creating Media File :

 

Now here comes our service class so that audio clip/music/song can be played using this class where we will be having…

 

–> Creating service class

 

 

–> Starting service i.e., playing audio file

 

 

–> Stopping service

 

 

 

4. Creating MainActivity File :

 

So the main functionality in MainActivity file is that we will be starting and ending a audio file using our created service class i.e.,  Media.class using two buttons “Start” And “Stop”.

 

–> Start /Play button

If you notice here we are starting service as

 

 

–> End /Stop button

If you notice here we are stopping service as

 

 

 

Full Code :

 

5. Output :

play1

Show Buttons
Hide Buttons
Read previous post:
Android Expandable Textview

Close