App That Changes Background And Audio Player In Android, Using Android Studio

Introduction

 
In this article, I will show you how to create an app that changes the background and default audio player of your Android phone. We will be using Android Studio to build the application. 
 
Requirements
Steps to be followed
 
Follow these steps to create an app that changes the background and audio player in Android phones. I have included the source code below.
 
Step 1
 
Open Android Studio and start a new Android Studio Project.
 
Audio Player Android App
 
Step 2
 
You can choose your application name and choose the location where your project is stored. If you wish to use C++ for coding the project, mark the "Include C++ support", and click the "Next" button.
 
Audio Player Android App
 
Step 3
 
Now, select the version of Android and select the target Android devices. We need to choose the SDK level which plays an important role to run the application.
 
Audio Player Android App
 
Step 4
 
Now, add the activity and click the "Next" button.
 
Audio Player Android App
 
Step 5
 
Add Activity name and click "Finish".
 
Audio Player Android App
 
Step 6
 
Add images in the Drawable file. The below template shows how to add the image file. Go to the app and right-click. “Show in Explorer” will appear. Then, click on the path and the image in the main folder.
 
Audio Player Android App
 
Audio Player Android App
 
Step 7
 
The below template is shown after adding the image file.
 
Audio Player Android App
 
Step 8
 
Go to activity_main.xml. This XML file contains the designing code for your Android app.
 
Audio Player Android App
 
The XML code is given below.
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <android.support.constraint.ConstraintLayout  
  3.  xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  5.     xmlns:tools="http://schemas.android.com/tools"  
  6.     android:layout_width="match_parent"  
  7.     android:layout_height="match_parent"  
  8.     android:background="@drawable/s"  
  9.     tools:context="abu.musicplayer.MainActivity">  
  10.   
  11.     <SeekBar  
  12.   
  13.         android:id="@+id/ss"  
  14.         android:layout_width="fill_parent"  
  15.         android:layout_height="wrap_content"  
  16.         tools:layout_editor_absoluteY="265dp"  
  17.         tools:layout_editor_absoluteX="0dp" />  
  18.   
  19.     <Button android:layout_height="wrap_content"  
  20.   
  21.         android:layout_width="fill_parent"  
  22.         android:id="@+id/bb"  
  23.         android:layout_below="@+id/ss"  
  24.         android:layout_alignRight="@+id/ss"  
  25.         android:text="Play"  
  26.         tools:layout_editor_absoluteY="290dp"  
  27.         tools:layout_editor_absoluteX="0dp" />  
  28.   
  29. </android.support.constraint.ConstraintLayout>  
Step 9
 
Go to Main Activity.java. This Java program is the backend language for Android.
 
Audio Player Android App
 
The Java code is given below.
  1. package abu.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.song);  
  26.         ss.setMax(mp.getDuration());  
  27.   
  28.         b.setOnClickListener(this)  
  29.         ss.setOnTouchListener(new OnTouchListener() {  
  30.             @Override  
  31.             public boolean onTouch(View v, MotionEvent event) {  
  32.                 // TODO Auto-generated method stub  
  33.                 SeekBar s=(SeekBar) v;  
  34.                 mp.seekTo(s.getProgress());  
  35.                 return false;  
  36.             }  
  37.         });  
  38.     }  
  39.   
  40.     @Override  
  41.     public boolean onCreateOptionsMenu(Menu menu) {  
  42.         // Inflate the menu; this adds items to the action bar if it is present.  
  43.         getMenuInflater().inflate(R.menu.main, menu);  
  44.         return true;  
  45.     }  
  46.     @Override  
  47.     public void onClick(View v) {  
  48.         // TODO Auto-generated method stub  
  49.         if (i==0  
  50.         {  
  51.             mp.start();  
  52.           add();  
  53.             b.setText("pause");  
  54.             i++;  
  55.         }  
  56.         else if(i==1)  
  57.         {  
  58.             mp.pause();  
  59.           b.setText("Resume");  
  60.            i++;  
  61.         }  
  62.         else if(i==2)  
  63.         {  
  64.             mp.start();  
  65.             add();  
  66.             b.setText("Pause");  
  67.             i=1;  
  68.         }  
  69.     }  
  70.     public void add()  
  71.     {  
  72.         ss.setProgress(mp.getCurrentPosition());  
  73.         Runnable r1=new Runnable() {  
  74.             @Override  
  75.             public void run() {  
  76.                 // TODO Auto-generated method stub  
  77.                 add();  
  78.             }  
  79.         };  
  80.         h1.postDelayed(r1, 4000);  
  81.     }  
  82.   
  83. }  
Step 10
 
Now, go to the menu bar and click "Make Project" or press ctrl+f9 to debug the error.
 
Audio Player Android App
 
Step 11
 
Then, click the "Run" button or press shift+f10 to run the project. And choose the "virtual machine" option and click OK.
 
Audio Player Android App
 

Conclusion

 
We have successfully changed the background and audio player in our Android app.
 
Audio Player Android App
 
Audio Player Android App
 
Audio Player Android App