title: Trying to make a pause/resume music activity using service
body: <p>I'm trying to create an activity with one button in its layout which will pause or resume the music when it's pressed. the problem is that when it's pressed to resume the music it starts the audio file all over again. Any idea how to make it play from the last point it stopped rather than restarting it?</p>
  
  <p><strong>Main activity</strong></p>
  
  <pre><code>public class MainActivity extends Activity implements OnClickListener {
  
      private Boolean isMusicPlaying;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          View musicButton = findViewById(R.id.music_button);
          musicButton.setOnClickListener(this);
          startService(new Intent(getBaseContext(), MusicService.class));
          isMusicPlaying = true;
      }
  
      @Override
      public void onClick(View v) {
          if (v.getId() == R.id.music_button)
              if (isMusicPlaying) stopService(new Intent(getBaseContext(),
                      MusicService.class));
              else startService(new Intent(getBaseContext(), MusicService.class));
          isMusicPlaying = !isMusicPlaying;
      }
  }
  </code></pre>
  
  <p><strong>Service class</strong></p>
  
  <pre><code>public class MusicService extends Service {
  
      MediaPlayer player;
  
      @Override
      public IBinder onBind(Intent arg0) {
          return null;
      }
  
      public void onCreate() {
          super.onCreate();
          player = MediaPlayer.create(this, R.raw.song);
          player.setLooping(true); // Set looping
          player.setVolume(100, 100);
      }
  
      public int onStartCommand(Intent intent, int flags, int startId) {
          player.start();
          return START_STICKY;
      }
  
      @Override
      public void onDestroy() {
          player.pause();
      }
  }
  </code></pre>