Implementing Media Player (Audio)

0
1310

Hello friends!! In this tutorial we are going to implement Multimedia to play Audio files.

  • All we will do in this tutorial is on clicking the buttons it will play the piano sound notes .
  • So follow the Steps :
    1. Create a new project and for example name it as Audio.
    2. Your MainActivity and activity_main files will be generated respectively.
    3. Now go to your activity_main.xml file and in your graphical Layout take 5 Buttons in series and name them as C,D…respectively and one button seperately in the layout name it play song .
    4. So now your activity_main.xml file graphical layout will as shown below :
    5. 1

    6. Now for each button give the following id’s and onClick method in the properties of the button as shown in the code :
    7. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="horizontal"
      tools:context=".MainActivity" >
      
             <Button
              android:id="@+id/button6"
              style="?android:attr/buttonStyleSmall"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_below="@+id/btnE"
              android:layout_marginTop="87dp"
             	android:layout_toLeftOf="@+id/btnF"
              android:text="Play Song" 
                android:onClick="playLocalAudio"/>
      
          <Button
              android:id="@+id/btnC"
              style="?android:attr/buttonStyleSmall"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_above="@+id/button6"
              android:layout_alignParentLeft="true"
              android:text="   C   " 
              android:onClick = "play"/>
      
          <Button
              android:id="@+id/btnD"
              style="?android:attr/buttonStyleSmall"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentTop="true"
              android:layout_marginLeft="16dp"
              android:layout_marginTop="60dp"
              android:layout_toRightOf="@+id/btnC"
              android:onClick="play"
              android:text="   D   " />
      
      
      
      
          <Button
              android:id="@+id/btnE"
              style="?android:attr/buttonStyleSmall"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignBaseline="@+id/btnD"
              android:layout_alignBottom="@+id/btnD"
              android:layout_marginLeft="16dp"
              android:layout_toRightOf="@+id/btnD"
              android:onClick="play"
              android:text="   E   " />
      
          <Button
              android:id="@+id/btnG"
              style="?android:attr/buttonStyleSmall"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_above="@+id/button6"
              android:layout_alignParentRight="true"
              android:layout_marginRight="48dp"
              android:onClick="play"
              android:text="   G   " />
      
          <Button
              android:id="@+id/btnF"
              style="?android:attr/buttonStyleSmall"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignBaseline="@+id/btnG"
              android:layout_alignBottom="@+id/btnG"
              android:layout_marginRight="16dp"
              android:layout_toLeftOf="@+id/btnG"
              android:onClick="play"
              android:text="  F   " />
      
      </RelativeLayout>
      
      
      

    8. Now after adding the properties your code will look as shown below :
    9. 2
      3
      4

    10. Now create a new folder in your res folder and name it raw and copy your audio files in it .
    11. 5

    12. Now create an sdcard in your emulator , to create an sdcard go to window ->Android Virtual Device Manager.
    13. Now select your emulator and click on Edit you will get the following window :
    14. 6

    15. Set the size of the sd Card to 2048 and click on OK.
    16. Now go to your DDMS prespective ->File Explorer -> go to mnt -> select sdcard -> add file in sdcard, do the steps as shown in diagram :
    17. 7

    18. Now go to your MainActivity.Java and write the following code :
    19. public class MainActivity extends Activity 
      {	
      	//create object for MediaPlayer as mp , local_mp :
      	
      	MediaPlayer mp = null;	//for playing from the /res/raw folder
      	MediaPlayer local_mp = null; //for playing from the sd card
      
      	@Override
      	protected void onCreate(Bundle savedInstanceState) {
      		super.onCreate(savedInstanceState);
      		setContentView(R.layout.activity_main);
      	}
      	//on pressing back button
      	public void onStop()
      	{
      		super.onStop();
      		
      	//If mp!=null then release mp and make it null so that it will stop :
      		if(mp!=null)
      		{
      			mp.release();
      			mp = null;
      		}
      		
      //If local_mp!=null then release local_mp and make it null so that it        //will stop 
      		if(local_mp!=null)
      		{
      			local_mp.release();
      			local_mp = null;
      		}
      	}
      	
      
      
      
      //Now onClick of the button as the onClick property is "play" it will //play the respective audio file : 
      
      	public void play(View v)
      	{
      		if(mp!=null)
      		{
      			mp.release();
      		}
      		
      		//get the respective Id's of the button :
      	switch(v.getId())
      	{
      	case R.id.btnC:
      		 mp = MediaPlayer.create(getApplicationContext(), R.raw.c);
      		 break;
      	case R.id.btnD:
      		 mp = MediaPlayer.create(getApplicationContext(), R.raw.d);
      		 break;
      	case R.id.btnE:
      		 mp = MediaPlayer.create(getApplicationContext(), R.raw.e);
      		 break;
      	case R.id.btnF:
      		 mp = MediaPlayer.create(getApplicationContext(), R.raw.f);
      		 break;
      	case R.id.btnG:
      		 mp = MediaPlayer.create(getApplicationContext(), R.raw.g);
      		 break;
      		
      	}
      	mp.start();
      }
      	
      	//the play song button’s  onClick property is playLocalAudio :
      
      	public void playLocalAudio(View v)
      	{
      		if(local_mp!=null)
      		{
      			local_mp.release();
      		}
      		//set the path of the audio file in the sdcard :
      		String sdPath = "/mnt/sdcard/katy.mid";
      		local_mp = new MediaPlayer();
      		try
      		{
      			local_mp.setDataSource(sdPath);
      			local_mp.prepare();
      			local_mp.start();	//start the MediaPlayer
      		}catch (IOException e)
      		{
      			e.printStackTrace();
      		}
      	}
      	
      }
      
      

    20. Your MainActivity.Java workspace will look as shown below :
    21. 8
      9
      10

    22. Now go to your AndroidManifest file and specify the permissions required to push the file to the sdcard :
    23. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
       <uses-permission  android:name="android.permission.READ_EXTERNAL_STORAGE"/>
      
      

      11

    24. Now run your application and you will get the following output :
    25. 12

    26. Now click on the respective button it will play the sound for you
    27. Thus we can now play AUDIO files in our application .