Zeeshan Chaudhry
Zeeshan Chaudhry

Reputation: 862

Android Background Service didn't stop/terminate

I am using a Service to play background Music. The problem is that the music continues playing when i have finished the activity.

Here is code From Main Activity which starts the service

            Intent svc=new Intent(HomeActivity.this, BackgroundSoundService.class);
    startService(svc);

BackgroundSoundService.java

         public class BackgroundSoundService extends Service {
             private static final String TAG = null;
             public static MediaPlayer player;
             public IBinder onBind(Intent arg0) {

                 return null;
             }
             @Override
             public void onCreate() {
                 super.onCreate();
                 Log.d("atMedia", "Backround Music playing");
                 player = MediaPlayer.create(this, R.raw.background);
                 player.setLooping(true); // Set looping
                 player.setVolume(100,100);

             }
             public int onStartCommand(Intent intent, int flags, int startId) {
                 player.start();
                 return 1;
             }

             public void onStart(Intent intent, int startId) {
                 // TO DO
             }
             public IBinder onUnBind(Intent arg0) {
                 // TO DO Auto-generated method
                 return null;
             }

             public void onStop() {

             }
             public void onPause() {

             }
             @Override
            public void onDestroy() {
                super.onDestroy();
                 player.stop();
                 player.release();
             }

             @Override
             public void onLowMemory() {

             }

}

Upvotes: 1

Views: 1456

Answers (2)

Leap Bun
Leap Bun

Reputation: 2295

Try to stop service from your MainActivity:

Intent svc=new Intent(HomeActivity.this, BackgroundSoundService.class);     
stopService(svc);

Upvotes: 3

Ramindu Weeraman
Ramindu Weeraman

Reputation: 354

You have to call selfStop() method inside your service. or use stopService() API

Upvotes: 3

Related Questions