Android MediaPlayer with Service, implement Pause

1

Goodbye everyone and thanks in advance, I'm creating a simple player in Android Studio that basically has three buttons, pause, play, stop. To be heard on the background I call the class MediaPlayer from a Service . Running and closing the service is simple but I'm not sure how to implement the pause. Any recommendations?

ThreadMusic Class containing Service with MediaPlayer:

  package start2develop.org.reproductormp3;

  import android.app.Service;
  import android.content.Intent;
  import android.media.MediaPlayer;
  import android.os.IBinder;



 public class HiloMusica extends Service {
MediaPlayer mp;
public HiloMusica(){}
public void onCreate(){
    mp=new MediaPlayer().create(this,R.raw.got);
}
public int onStartCommand(Intent i, int flags, int
        idArranque){
    mp.start();
    return START_STICKY;
}

public void onDestroy(){
    mp.stop();mp.release();
}
public IBinder onBind(Intent intent) {
    return null;
} }

MainActivity with the necessary methods for play, pause, stop:

   package start2develop.org.reproductormp3;

   import android.content.Intent;
   import android.media.MediaPlayer;
   import android.support.v7.app.AppCompatActivity;
   import android.os.Bundle;
   import android.view.View;
   import android.widget.Button;

   import java.io.IOException;

    public class MainActivity extends AppCompatActivity implements 
      View.OnClickListener {

Button btPlay;
Button btPause;
Button btStop;
@Override
protected void onCreate(Bundle savedInstanceState) {
    Intent i = new Intent(this, HiloMusica.class);
    startService(i);
    stopService(i);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

   // mp = MediaPlayer.create(this,R.raw.got);
   // mp.setOnPreparedListener(this);
    //mp.setOnCompletionListener(this);
    btPlay = (Button)findViewById(R.id.btPlay);
    btPause = (Button)findViewById(R.id.btPause);
    btStop = (Button)findViewById(R.id.btStop);
    btPlay.setOnClickListener(this);
    btPause.setOnClickListener(this);
    btStop.setOnClickListener(this);
   // enableButton(false,false,false);



}

   void enableButton (boolean btPlay, boolean btPause, boolean
        btStop){
    this.btPlay.setEnabled(btPlay);
    this.btPause.setEnabled(btPause);
    this.btStop.setEnabled(btStop);
    }
    public void play (View v){
    Intent i = new Intent(this, HiloMusica.class);
    startService(i);}
    //enableButton(false,true,true);

   public void stop (View v){
    Intent i = new Intent(this, HiloMusica.class);
    stopService(i);
    //enableButton(false,true,true);
        }
   public void pause (View v){
    //?
    }

   /*enableButton(true,false,true);
    }
    public void mediaSelected(View v){
    try {
        mp.setDataSource("C:\Users\aer\Desktop\bbt.mp3");
        mp.prepare();//Async();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
  public void onCompletion(MediaPlayer mp) {
    enableButton(true,false,false);
}
  public void onPrepared(MediaPlayer mp) {
    enableButton(true,false,false);
}*/

public void onClick(View v) {
    switch (v.getId()) {
        case R.id.btPlay:
            play(v);
            break;
        case R.id.btPause:
            pause(v);
            break;
        case R.id.btStop:
            stop(v);
            break;
    }}
}
    
asked by Raul.Rt 27.02.2018 в 13:31
source

1 answer

-1

To implement pause the MediaPlayer must be playing, therefore simply detect if you are doing this action and use the method pause ()

 public void pause (View v){

       if(mp.isPlaying()){
          mp.pause();
        }

  }
    
answered by 27.02.2018 в 17:14