Make Your Own Music Player in Android

Make your own Music Player in Android

 
Procedure
  1. Start Eclipse IDE.
  2. Create a new project.
  3. Create a MainActivity.java file.
  4. Create an activity_main.xml file for layout design.
  5. Add an XML file, a Button and a SeekBar.
  6. Create a new folder in "res", named "raw".
  7. Put the song in the "raw" folder with .mp3 extension.
  8. The code is given below.
MainActivity.java
  1. package com.example.musicplayer;  
  2. import android.media.MediaPlayer;  
  3. import android.os.Bundle;  
  4. import android.os.Handler;  
  5. import android.app.Activity;  
  6. import android.view.Menu;  
  7. import android.view.MotionEvent;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.view.View.OnTouchListener;  
  11. import android.widget.Button;  
  12. import android.widget.SeekBar;  
  13.    
  14. public class MainActivity extends Activity implements OnClickListener {  
  15.    
  16.        SeekBar ss;  
  17.        Button b;  
  18.        MediaPlayer mp;  
  19.        Handler h1=new Handler();  
  20.        int i=0;  
  21.    
  22.        @Override  
  23.        protected void onCreate(Bundle savedInstanceState) {  
  24.               super.onCreate(savedInstanceState);  
  25.               setContentView(R.layout.activity_main);  
  26.               ss=(SeekBar)findViewById(R.id.ss);  
  27.               b=(Button)findViewById(R.id.bb);  
  28.               mp=MediaPlayer.create(getApplicationContext(), R.raw.aashiqui);  
  29.               ss.setMax(mp.getDuration());  
  30.               b.setOnClickListener(this);  
  31.               ss.setOnTouchListener(new OnTouchListener() {  
  32.                      @Override  
  33.                      public boolean onTouch(View v, MotionEvent event) {  
  34.                            // TODO Auto-generated method stub  
  35.                            SeekBar s=(SeekBar) v;  
  36.                            mp.seekTo(s.getProgress());  
  37.                            return false;  
  38.                      }  
  39.               });  
  40.        }  
  41.        @Override  
  42.        public boolean onCreateOptionsMenu(Menu menu) {  
  43.               // Inflate the menu; this adds items to the action bar if it is present.  
  44.               getMenuInflater().inflate(R.menu.main, menu);  
  45.               return true;  
  46.        }  
  47.        @Override  
  48.        public void onClick(View v) {  
  49.               // TODO Auto-generated method stub  
  50.               if (i==0)  
  51.               {  
  52.                      mp.start();  
  53.                      add();  
  54.                      b.setText("pause");  
  55.                      i++;  
  56.               }  
  57.               else if(i==1)  
  58.               {  
  59.                      mp.pause();  
  60.                      b.setText("Resume");  
  61.                      i++;  
  62.               }  
  63.               else if(i==2)  
  64.               {  
  65.                      mp.start();  
  66.                      add();  
  67.                      b.setText("Pause");  
  68.                      i=1;  
  69.               }  
  70.           }  
  71.        public void add()  
  72.        {  
  73.               ss.setProgress(mp.getCurrentPosition());  
  74.    
  75.               Runnable r1=new Runnable() {  
  76.    
  77.                      @Override  
  78.                      public void run() {  
  79.                            // TODO Auto-generated method stub  
  80.                            add();  
  81.                      }  
  82.        };  
  83.        h1.postDelayed(r1, 4000);  
  84.        }  
  85.        } 
activity_main.xml
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2. xmlns:tools="http://schemas.android.com/tools"  
  3. android:layout_width="match_parent"  
  4. android:layout_height="match_parent"  
  5. android:paddingBottom="@dimen/activity_vertical_margin"  
  6. android:paddingLeft="@dimen/activity_horizontal_margin"  
  7. android:paddingRight="@dimen/activity_horizontal_margin"  
  8. android:paddingTop="@dimen/activity_vertical_margin"  
  9. tools:context=".MainActivity" >  
  10.   <SeekBar  
  11.   android:id="@+id/ss"  
  12.   android:layout_width="fill_parent"  
  13.   android:layout_height="wrap_content"  
  14. />  
  15.   <Button android:layout_height="wrap_content"  
  16.   android:layout_width="fill_parent"  
  17.   android:id="@+id/bb"  
  18.   android:layout_below="@+id/ss"  
  19.   android:layout_alignRight="@+id/ss"  
  20.   android:text="Play"  
  21. />  </RelativeLayout>
Output