Tuesday 31 January 2017

How to Run Audio File when you Click on Image Button in Android

Here I am sharing explanation of layout and coding ...

Step 1: Create new project and do basic activities..
Step 2 : Go to drawable folder and paste .jpg file over there. 
Step 3 : Go to res folder, Create raw folder, paste .mp3 file over there. 

Step 4 : Pasting here screen shot


Step 4 : Go to MainActivity.java and code is below


   package com.example.username.runaudio;

Explanation of Layout - 

1 . Library created automatically
2.  Dragging and dropping of imagebutton (widgest from palette).
3. saving image file (pink.jpg) on drawable folder. ( which is inside res folder)
4. created raw folder to save audio file. (inside res folder)and paste itspink.mp3 file over
   there.
 
Explanation of code - 

1. declare variables inside main method
2. created one method clickImageButton() ( we need to create listner when we press on 
imagebutton.    in this method we casted (assign) our widgest. 
3. we need to create onClickListner on our click action and we created this on code and there
 onclick() method is created and this is bydefault created by eclips. 

4. Now MediaPlayer.create method fetches audio file from raw folder and starts audio by calling 
method mdp.start()