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 phone. I have included the source code below.
Step 1
Open Android Studio and start a new Android Studio Project.
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 "Next" button.
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.
Step 4
Now, add the activity and click "Next" button.
Step 5
Add Activity name and click "Finish".
Step 6
Add images in 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.
Step 7
The below template is shown after adding the image file.
Step 8
Go to activity_main.xml. This XML file contains the designing code for your Android app.
The XML code is given below.
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.constraint.ConstraintLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@drawable/s"
- tools:context="abu.musicplayer.MainActivity">
-
- <SeekBar
-
- android:id="@+id/ss"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- tools:layout_editor_absoluteY="265dp"
- tools:layout_editor_absoluteX="0dp" />
-
- <Button android:layout_height="wrap_content"
-
- android:layout_width="fill_parent"
- android:id="@+id/bb"
- android:layout_below="@+id/ss"
- android:layout_alignRight="@+id/ss"
- android:text="Play"
- tools:layout_editor_absoluteY="290dp"
- tools:layout_editor_absoluteX="0dp" />
-
- </android.support.constraint.ConstraintLayout>
Step 9
Go to Main Activity.java. This Java program is the backend language for Android.
The Java code is given below.
- package abu.musicplayer;
- import android.media.MediaPlayer;
- import android.os.Bundle;
- import android.os.Handler;
- import android.app.Activity;
- import android.view.Menu;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.View.OnTouchListener;
- import android.widget.Button;
- import android.widget.SeekBar;
- public class MainActivity extends Activity implements OnClickListener {
- SeekBar ss;
- Button b;
- MediaPlayer mp;
- Handler h1=new Handler();
- int i=0;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- ss=(SeekBar)findViewById(R.id.ss);
- b=(Button)findViewById(R.id.bb);
- mp=MediaPlayer.create(getApplicationContext(), R.raw.song);
- ss.setMax(mp.getDuration());
-
- b.setOnClickListener(this)
- ss.setOnTouchListener(new OnTouchListener() {
- @Override
- public boolean onTouch(View v, MotionEvent event) {
-
- SeekBar s=(SeekBar) v;
- mp.seekTo(s.getProgress());
- return false;
- }
- });
- }
-
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
-
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- @Override
- public void onClick(View v) {
-
- if (i==0
- {
- mp.start();
- add();
- b.setText("pause");
- i++;
- }
- else if(i==1)
- {
- mp.pause();
- b.setText("Resume");
- i++;
- }
- else if(i==2)
- {
- mp.start();
- add();
- b.setText("Pause");
- i=1;
- }
- }
- public void add()
- {
- ss.setProgress(mp.getCurrentPosition());
- Runnable r1=new Runnable() {
- @Override
- public void run() {
-
- add();
- }
- };
- h1.postDelayed(r1, 4000);
- }
-
- }
Step 10
Now, go to menu bar and click "Make Project" or press ctrl+f9 to debug the error.
Step 11
Then, click "Run" button or press shift+f10 to run the project. And choose the "virtual machine" option and click OK.
Conclusion
We have successfully changed the background and audio player in our Android app.