title: How can I control the media player with service?
body: <p>I am new to coding and at this point, I made a media player that starts and pauses the audio, has a working seekbar, duration, etc. Now I faced a big issue. The audio can't be played in the background and I found out that I can do this with service but this changes things. I read all kinds of topics on how to control the audio in service with seekbar and all kinds of stuff but nothing helped me. The main problem I face is having the seekbar to control the audio and a text to read the time. If there is someone to help me find a code for this it would be much appreciated.</p>
  <p><a href="https://i.stack.imgur.com/LKy8d.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LKy8d.jpg" alt="" /></a></p>
  <p>The layout:</p>
  <pre><code> &lt;SeekBar
      android:id=&quot;@+id/seekBarMusic1&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;@dimen/_20sdp&quot;
      android:layout_marginTop=&quot;@dimen/_12sdp&quot;
      android:layout_marginLeft=&quot;@dimen/_20sdp&quot;
      android:layout_marginRight=&quot;@dimen/_20sdp&quot;
      app:layout_constraintTop_toBottomOf=&quot;@+id/cardView&quot;
      tools:layout_editor_absoluteX=&quot;10dp&quot; /&gt;
  
  &lt;TextView
      android:id=&quot;@+id/playerPositionMusic&quot;
      android:layout_width=&quot;wrap_content&quot;
      android:layout_height=&quot;wrap_content&quot;
      android:text=&quot;00:00&quot;
      android:textColor=&quot;@color/remain_black&quot;
      android:textSize=&quot;25dp&quot;
      android:layout_marginBottom=&quot;@dimen/_80sdp&quot;
      app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
      app:layout_constraintStart_toStartOf=&quot;@+id/cardView&quot;
      app:layout_constraintTop_toBottomOf=&quot;@+id/seekBarMusic1&quot; /&gt;
  
  &lt;TextView
      android:id=&quot;@+id/text_time&quot;
      android:layout_width=&quot;wrap_content&quot;
      android:layout_height=&quot;wrap_content&quot;
      android:layout_marginBottom=&quot;@dimen/_80sdp&quot;
      android:text=&quot;15:00&quot;
      android:textColor=&quot;@color/remain_black&quot;
      android:textSize=&quot;25dp&quot;
      app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
      app:layout_constraintEnd_toEndOf=&quot;@+id/cardView&quot;
      app:layout_constraintTop_toBottomOf=&quot;@+id/seekBarMusic1&quot; /&gt;
  
  &lt;ImageView
      android:id=&quot;@+id/btPlayMusic&quot;
      android:layout_width=&quot;80dp&quot;
      android:layout_height=&quot;80dp&quot;
      android:alpha=&quot;0.9&quot;
      android:background=&quot;@drawable/play&quot;
      android:theme=&quot;@style/Button.White&quot;
      app:layout_constraintEnd_toStartOf=&quot;@+id/text_time&quot;
      app:layout_constraintStart_toEndOf=&quot;@+id/playerPositionMusic&quot;
      app:layout_constraintTop_toBottomOf=&quot;@+id/seekBarMusic1&quot;
      app:tint=&quot;@color/remain_black&quot; /&gt;
  
   &lt;ImageView
      android:visibility=&quot;gone&quot;
      android:id=&quot;@+id/btPauseMusic&quot;
      android:layout_width=&quot;80dp&quot;
      android:layout_height=&quot;80dp&quot;
      android:alpha=&quot;0.9&quot;
      android:background=&quot;@drawable/pause&quot;
      android:theme=&quot;@style/Button.White&quot;
      app:layout_constraintEnd_toStartOf=&quot;@+id/text_time&quot;
      app:layout_constraintStart_toEndOf=&quot;@+id/playerPositionMusic&quot;
      app:layout_constraintTop_toBottomOf=&quot;@+id/seekBarMusic1&quot;
      app:tint=&quot;@color/remain_black&quot; /&gt;
  </code></pre>
  <p>The code I used before:</p>
  <pre><code>public class m1 extends AppCompatActivity {
  
  ImageView btPlay, btPause;
  TextView playerPosition, playerDuration;
  CircularSeekBar seekBar;
  
  MediaPlayer mediaPlayer;
  Handler handler = new Handler();
  Runnable runnable;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_m1);
  
      findViewById(R.id.backm1).setOnClickListener(v -&gt; onBackPressed());
  
      playerPosition = findViewById(R.id.playerPosition);
      playerDuration = findViewById(R.id.playerDuration);
      replay = findViewById(R.id.replay);
      forward = findViewById(R.id.forward);
      seekBar = findViewById(R.id.seekBar);
      btPause = findViewById(R.id.btPause);
      btPlay = findViewById(R.id.btPlay);
  
          mediaPlayer = MediaPlayer.create(this,R.raw.ding_dong);
  
      runnable = new Runnable() {
          @Override
          public void run() {
              seekBar.setProgress(mediaPlayer.getCurrentPosition());
  
              handler.postDelayed(this,500);
          }
      };
  
      int duration = mediaPlayer.getDuration();
  
    String sDuration = convertFormat(duration);
  
    playerDuration.setText(sDuration);
  
    btPlay.setOnClickListener(v -&gt; {
  
        mediaPlayer.start();
  
        btPlay.setVisibility(View.GONE);
  
        btPause.setVisibility(View.VISIBLE);
  
        seekBar.setMax(mediaPlayer.getDuration());
  
        handler.postDelayed(runnable, 0)
  
    });
  
    btPause.setOnClickListener(v -&gt; {
  
        mediaPlayer.stop();
  
        btPause.setVisibility(View.GONE);
  
        btPlay.setVisibility(View.VISIBLE);
  
  
        handler.removeCallbacks(runnable);
  
    });
  
  
    seekBar.setOnSeekBarChangeListener(new CircularSeekBar.OnCircularSeekBarChangeListener() {
        @Override
        public void onProgressChanged(CircularSeekBar circularSeekBar, float v, boolean b) {
  
            if (b) {
  
                mediaPlayer.seekTo((int) v);
  
            }
            playerPosition.setText(convertFormat(mediaPlayer.getCurrentPosition()));
        }
  
        @Override
        public void onStopTrackingTouch(CircularSeekBar circularSeekBar) {
  
        }
  
        @Override
        public void onStartTrackingTouch(CircularSeekBar circularSeekBar) {
  
        }
    });
  
    mediaPlayer.setOnCompletionListener(mp -&gt; {
        btPause.setVisibility((View.GONE));
  
        btPlay.setVisibility(View.VISIBLE);
  
        mediaPlayer.seekTo(0);
    });
  
      }
  }
  @SuppressLint(&quot;DefaultLocale&quot;)
      private String convertFormat(int duration) {
          return String.format(&quot;%02d:%02d&quot;
                  ,TimeUnit.MILLISECONDS.toMinutes(duration)
                  ,TimeUnit.MILLISECONDS.toSeconds(duration) -
                  TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(duration)));
  }
  @Override
  public void onPause()
  {
      if(mediaPlayer != null &amp;&amp; mediaPlayer.isPlaying())
      {
          mediaPlayer.stop();
      }
      super.onPause();
  }
  
  } 
  </code></pre>
  <p>The code I am using now:</p>
  <pre><code> btPause = findViewById(R.id.btPauseMusic);
      btPlay = findViewById(R.id.btPlayMusic);
  
      btPlay.setOnClickListener(v -&gt; {
  
          startService(new Intent(this, BackgroundMusicService.class));
  
          btPlay.setVisibility(View.GONE);
  
          btPause.setVisibility(View.VISIBLE);
      });
  
      btPause.setOnClickListener(v -&gt; {
  
          stopService(new Intent(this, BackgroundMusicService.class));
  
          btPause.setVisibility(View.GONE);
  
          btPlay.setVisibility(View.VISIBLE);
      });
  
  }
  </code></pre>
  <p>And the service class:</p>
  <pre><code>public class BackgroundMusicService extends Service {
  
  private MediaPlayer mediaPlayer;
  
  @Nullable
  @Override
  public IBinder onBind(Intent intent) {
      return null;
  }
  
  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
      mediaPlayer = MediaPlayer.create(this, R.raw.ding_dong);
      mediaPlayer.start();
      return START_STICKY;
  }
  
  @Override
  public void onDestroy() {
      super.onDestroy();
  
      mediaPlayer.stop();
  }
  }
  </code></pre>