Play a Song Using Service in Android

Introduction

 
In this article I will describe services in Android. A service is an application component that is run in the background without user interaction. If we want to add a service to our application then we must add a declaration in the "AndroidManifest.xml" file and the service class must be extended. In this article, I will tell you how to play music in the background using a service. In this article, I took a MP3 song file that must play using a service in the app.
 
Step 1
 
As usual first of all create a new project as "File" -> "New" -> "Android application Project".
 
NwPrjct.xml.jpg
 
Step 2
 
After that, import an audio file; I imported a MP3 file in my app as shown in the following image.
 
radha.jpg
 
Step 3
 
Now open the "actiivity_main.xml" file and update it with the following code:
  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.     tools:context=".MainActivity"  
  6.     android:background="#0ff0ff" >  
  7.     <TextView  
  8.         android:id="@+id/song"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_alignParentBottom="true"  
  12.         android:layout_alignParentLeft="true"  
  13.         android:layout_alignParentRight="true"  
  14.         android:layout_marginBottom="208dp"  
  15.         android:background="#f00f00"  
  16.         android:gravity="center"  
  17.         android:textSize="40sp"  
  18.         android:text="@string/play"  
  19.         android:textColor="#00f00f" />  
  20. </RelativeLayout> 
Step 4
 
Now open the "MainActivity.java" file and update it with the following code.
  1. package com.demo.service;  
  2. import android.app.Activity;  
  3. import android.content.Intent;  
  4. import android.os.Bundle;  
  5. import android.view.Menu;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.TextView;  
  9. public class MainActivity extends Activity {  
  10.       Intent intent;  
  11.       @Override  
  12.       protected void onCreate(Bundle savedInstanceState) {  
  13.             super.onCreate(savedInstanceState);  
  14.             setContentView(R.layout.activity_main);  
  15.             final TextView tv= (TextView) findViewById(R.id.song);  
  16.             intent=new Intent(this,MyService.class);  
  17.             tv.setOnClickListener(new OnClickListener() {  
  18.                   @Override  
  19.                   public void onClick(View v) {  
  20.                         // TODO Auto-generated method stub  
  21.                         tv.setText(R.string.playsongs);  
  22.                         startService(intent);  
  23.                   }  
  24.             });  
  25.       }  
  26.       @Override  
  27.       protected void onPause() {  
  28.             // TODO Auto-generated method stub  
  29.             stopService(intent);  
  30.             super.onPause();  
  31.       }  
  32.       @Override  
  33.       public boolean onCreateOptionsMenu(Menu menu) {  
  34.             // Inflate the menu; this adds items to the action bar if it is present.  
  35.             getMenuInflater().inflate(R.menu.activity_main, menu);  
  36.             return true;  
  37.       }  

Step 5
 
Create a new Java file extending the Service as "src/com.demo.service/MyService.java" and update it with the following code:
  1. package com.demo.service;  
  2. import java.io.IOException;  
  3. import android.app.Service;  
  4. import android.content.Intent;  
  5. import android.media.MediaPlayer;  
  6. import android.os.IBinder;  
  7. public class MyService extends Service {  
  8.       MediaPlayer media;  
  9.       @Override  
  10.       public IBinder onBind(Intent intent) {  
  11.             // TODO Auto-generated method stub  
  12.             return null;  
  13.       }  
  14.       @Override  
  15.       public void onCreate() {  
  16.             // TODO Auto-generated method stub  
  17.             super.onCreate();  
  18.       }  
  19.       @Override  
  20.       public int onStartCommand(Intent intent, int flags, int startId) {  
  21.             media=MediaPlayer.create(this, R.raw.radha);  
  22.             try {  
  23.                   media.prepare();  
  24.             } catch (IllegalStateException e) {  
  25.                   // TODO Auto-generated catch block  
  26.                   e.printStackTrace();  
  27.             } catch (IOException e) {  
  28.                   // TODO Auto-generated catch block  
  29.                   e.printStackTrace();  
  30.             }  
  31.             media.start();  
  32.             // TODO Auto-generated method stub  
  33.             return super.onStartCommand(intent, flags, startId);  
  34.       }  
  35.       @Override  
  36.       public void onDestroy() {  
  37.             // TODO Auto-generated method stub  
  38.             media.release();  
  39.             super.onDestroy();  
  40.       }  

Step 6
 
Now open the "/androidManifest.xml" file and update it with the service in the application as in the following code:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.demo.service"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.     <uses-sdk  
  7.         android:minSdkVersion="8"  
  8.         android:targetSdkVersion="17" />  
  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.demo.service.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="MyService">  
  23.        </service>  
  24.     </application>  
  25. </manifest> 
Step 7
 
Click on "Play" and listen to the song "radha.mp3". Here if you click on the text "Play" then the song will  play and if you click on the text "Pause" then the song will stop due to the service stopping.
 
Play
 
play_tv.xml.jpg
 
Pause
 
pause_tv.xml.jpg


Similar Articles