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



Share article on

Show Buttons
Hide Buttons

June 25, 2017

Attention Required! | Cloudflare

One more step

Please complete the security check to access push.wppush.co

Why do I have to complete a CAPTCHA?

Completing the CAPTCHA proves you are a human and gives you temporary access to the web property.

What can I do to prevent this in the future?

If you are on a personal connection, like at home, you can run an anti-virus scan on your device to make sure it is not infected with malware.

If you are at an office or shared network, you can ask the network administrator to run a scan across the network looking for misconfigured or infected devices.

Clear all
Read previous post:
expandablebanner
Android Expandable Textview

Close