Endi Tóth
Endi Tóth

Reputation: 221

Stop the music and the Service too

I'm trying to play a music using service in Android. What I'm struggling with is to stop the service and the music. Here is my service:

MediaPlayer mediaPlayer;

public SoundService() {
    super("SoundService");
}

@Override
public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.
    throw new UnsupportedOperationException("Not yet implemented");
}

@Override
protected void onHandleIntent(Intent intent) {

}

@Override
public void onCreate() {
    super.onCreate();
    mediaPlayer = MediaPlayer.create(getApplicationContext(),R.raw.zenenegy);
}


@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    mediaPlayer.start();
    mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            mp.release();
        }
    });
    return START_STICKY;
}

public void stopp(){
    Log.d("SERVICE","STOP");
    stopSelf();
}

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

When I call the 'stopp' method I got the Log so the service should have stopped, but the music went on. I have no idea how to stop the music because when I tried to use mediPlayer.stop(); method I have always got error messages so it would be great if I could just stop the service whenever I want to. Here is the code of the Activity :

public Button zene;
public boolean hang = true;
SoundService soundService;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fomenu);

    if(hang){
        startService();
    }
}

@Override
protected void onResume() {
    zene = (Button)findViewById(R.id.zene);

    zene.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                if(hang){
                    soundService.stopp();
                    //soundService.onDestroy();
                }
            hang = !hang;
        }
    });
    super.onResume();
}

public void startService(){
    startService(new Intent(getBaseContext(), SoundService.class));
}

public void stopService(){
    stopService(new Intent(getBaseContext(),SoundService.class));
}

}

I've tried to use stopService(); method too but it didn't work. If anyone has an idea how to stop the music in the service please response.

Upvotes: 1

Views: 845

Answers (2)

gaara87
gaara87

Reputation: 2077

SoundService.java

public void stop(){
  Log.d("SERVICE","STOP");
  mp.stop();
  //release the media player if you want to
  stopSelf();
}

A few ways of stopping would be to use Bound Services or in your onStartCommand recognize the intent action and then call stop()

It would look something like this

InYourActivity.java

public void stopService(){
    Intent stopIntent = new Intent(getBaseContext(),SoundService.class);
    intent.setAction(SoundService.ACTION_STOP);
    stopService(stopIntent);
}

SoundService.java

public static final String ACTION_STOP = "stop_music_service";

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if(intent.getAction()!=null && intent.getAction().equals(ACTION_STOP)){
       stop();
    }
    mediaPlayer.start();
    mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            mp.release();
        }
    });
    return START_STICKY;
}

Upvotes: 1

joshgoldeneagle
joshgoldeneagle

Reputation: 4646

You need to call the stop method for the instance of the MediaPlayer object you have instantiated. In your case your MediaPlayer object is called mediaPlayer.

Here is an example:

public void stopMusic(){
 if (mediaPlayer != null && mediaPlayer.isPlaying()){
   mediaPlayer.stop();
   mediaPlayer.destroy();
 }
}

Upvotes: 0

Related Questions