Learn How to Create Audio Player in Android

Introduction

 
This article explains the Audio Player in Android.
 
In this application, we will start audio using the service component. In this, you will first extend the service class that provides the methods of the service lifecycle.
 
The service lifecycle has the following methods:
  • onStartCommand(): when the component like an Activity requests a service to start by calling the startService() method then the system calls the onStartCommand() method to start the service.
  • onCreate(): this method is called by the system when the service is first created.
  • onDestroy(): this method is called when the service is no longer needed.
Create a project as in the following:
 
CreateAudioPlayer
 
 
 
 
 
Step 1
 
Create an XML file and write this. In this, you will use two buttons inside the RelativeLayout.
  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.     <Button  
  6.         android:id="@+id/button1"  
  7.         android:layout_width="fill_parent"  
  8.         android:layout_height="wrap_content"  
  9.         android:onClick="pAudio"  
  10.         android:text="Play" />  
  11.     <Button  
  12.         android:layout_marginTop="80dp"  
  13.         android:id="@+id/button2"  
  14.         android:layout_width="fill_parent"  
  15.         android:layout_height="wrap_content"  
  16.         android:onClick="sAudio"  
  17.         android:text="Stop" />  
  18. </RelativeLayout> 
Step 2
 
Create a Java class file with the following:
  1. import android.os.Bundle;  
  2. import android.app.Activity;  
  3. import android.view.Menu;  
  4. import android.view.View;  
  5. import android.content.Intent;  
  6.   
  7. //In this class, you will extend the activity class that provides methods to create the activity  
  8. public class MainActivity extends Activity   
  9. {  
  10.  @Override  
  11.  public void onCreate(Bundle savedInstanceState)   
  12.  {  
  13.   super.onCreate(savedInstanceState);  
  14.   setContentView(R.layout.activity_main);  
  15.  }  
  16.  @Override  
  17.  public boolean onCreateOptionsMenu(Menu menu)   
  18.  {  
  19.   getMenuInflater().inflate(R.menu.main, menu);  
  20.   return true;  
  21.  }  
  22.  //, when you click on the play button pAudio() method, will be called and it will call the Audio class that starts the service and startService()  
  23.  public void pAudio(View v)   
  24.  {  
  25.   Intent Intent = new Intent(this, Audio.class);  
  26.   startService(Intent);  
  27.  }  
  28.  //, when you click on the stop button sVideo() method, will be called and it will call the Audio class that starts the service and call stopService()  
  29.  public void sAudio(View v)   
  30.  {  
  31.   Intent Intent = new Intent(this, Audio.class);  
  32.   stopService(Intent);  
  33.  }  
Step 3
 
Create a Java class file with the following:
  1. package com.audioplayer;  
  2. import android.app.Service;  
  3. import android.content.Intent;  
  4. import android.media.MediaPlayer;  
  5. import android.os.IBinder;  
  6. import android.util.Log;  
  7.   
  8. // In this, you will extend the Service class that provides you the service lifecycle methods  
  9. public class Audio extends Service   
  10. {  
  11.  private static final String LOGCAT = null;  
  12.  MediaPlayer mediaPlayer;  
  13.  public void onCreate()   
  14.  {  
  15.   super.onCreate();  
  16.   Log.d(LOGCAT, "Service Started!");  
  17.   mediaPlayer = MediaPlayer.create(this, R.raw.beep);  
  18.  }  
  19.  public int onStartCommand(Intent intent, int flags, int startId)  
  20.  {  
  21.   mediaPlayer.start();  
  22.   Log.d(LOGCAT, "Media Player started!");  
  23.   if (mediaPlayer.isLooping() != true)  
  24.   {  
  25.    Log.d(LOGCAT, "Problem in Playing Audio");  
  26.   }  
  27.   return 1;  
  28.  }  
  29.  public void onStop()  
  30.  {  
  31.   mediaPlayer.stop();  
  32.   mediaPlayer.release();  
  33.  }  
  34.  public void onPause()  
  35.  {  
  36.   mediaPlayer.stop();  
  37.   mediaPlayer.release();  
  38.  }  
  39.   
  40.  public void onDestroy()  
  41.  {  
  42.   mediaPlayer.stop();  
  43.   mediaPlayer.release();  
  44.  }  
  45.  @Override  
  46.  public IBinder onBind(Intent objIndent)  
  47.  {  
  48.   return null;  
  49.  }  
Step 4
 
In the Android Manifest.xml file you will declare the service as in the following:
  1. <service android:name="Audio" android:enabled="true"></service> 
Android Manifest.xml file
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.audioplayer"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.     <uses-sdk  
  7.         android:minSdkVersion="8"  
  8.         android:targetSdkVersion="18" />  
  9.     <application  
  10.         android:allowBackup="true"  
  11.         android:icon="@drawable/ic_launcher"  
  12.         android:label="@string/app_name"  
  13.         android:theme="@style/AppTheme" >  
  14.         <activity  
  15.             android:name="com.audioplayer.MainActivity"  
  16.             android:label="@string/app_name" >  
  17.             <intent-filter>  
  18.                 <action android:name="android.intent.action.MAIN" />  
  19.                 <category android:name="android.intent.category.LAUNCHER" />  
  20.             </intent-filter>  
  21.         </activity>  
  22.         <service android:name="Audio" android:enabled="true"></service>  
  23.     </application>  
  24. </manifest> 
Step 5