Creating a MediaPlayer in a Service

Create a class which extends Service, in that in onCreate(), create the mediaPlayer and if we need, we can provide a notification also, to notify the user that a background service is being activated.

In onStart(), start(), the mediaplayer also, set looping equals true. So that the mediaplayer will play continously

The complete code is

public class AudioService extends Service {

Mediaplayer player;
public void onCreate() {
player = MediaPlayer.create(this, R.raw.mj);
NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
nOTIFICATION NOTIFICATION = new Notification(0, "Player started", System.currentTimeMillis());
nm.notify(0, notification);
} catch(Exception e){

public void onStart(Intent intent, int startId) {

public IBinder onBind(Intent intent) {
return null;


Popular posts from this blog

Open Gallery and get the selected image in appplication

Dismiss or Cancel dialog by swipe gesture

AlarmManager and Notification in Android