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. public class MainActivity extends Activity implements OnClickListener {
  14. SeekBar ss;
  15. Button b;
  16. MediaPlayer mp;
  17. Handler h1=new Handler();
  18. int i=0;
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.activity_main);
  23. ss=(SeekBar)findViewById(R.id.ss);
  24. b=(Button)findViewById(R.id.bb);
  25. mp=MediaPlayer.create(getApplicationContext(), R.raw.aashiqui);
  26. ss.setMax(mp.getDuration());
  27. b.setOnClickListener(this);
  28. ss.setOnTouchListener(new OnTouchListener() {
  29. @Override
  30. public boolean onTouch(View v, MotionEvent event) {
  31. // TODO Auto-generated method stub
  32. SeekBar s=(SeekBar) v;
  33. mp.seekTo(s.getProgress());
  34. return false;
  35. }
  36. });
  37. }
  38. @Override
  39. public boolean onCreateOptionsMenu(Menu menu) {
  40. // Inflate the menu; this adds items to the action bar if it is present.
  41. getMenuInflater().inflate(R.menu.main, menu);
  42. return true;
  43. }
  44. @Override
  45. public void onClick(View v) {
  46. // TODO Auto-generated method stub
  47. if (i==0)
  48. {
  49. mp.start();
  50. add();
  51. b.setText("pause");
  52. i++;
  53. }
  54. else if(i==1)
  55. {
  56. mp.pause();
  57. b.setText("Resume");
  58. i++;
  59. }
  60. else if(i==2)
  61. {
  62. mp.start();
  63. add();
  64. b.setText("Pause");
  65. i=1;
  66. }
  67. }
  68. public void add()
  69. {
  70. ss.setProgress(mp.getCurrentPosition());
  71. Runnable r1=new Runnable() {
  72. @Override
  73. public void run() {
  74. // TODO Auto-generated method stub
  75. add();
  76. }
  77. };
  78. h1.postDelayed(r1, 4000);
  79. }
  80. }
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