javaandroidandroid-servicebackground-music

Change background music volume from activity to service


I'm new to android, and working on a project where I have to implement background music in our app. I've already implemented background music using service, the problem is: I don't know how to change the volume of the music dynamically using a seekbar from an activity.

My service looks like this:

package hu.szoftverprojekt.holdemfree.controller;

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

import androidx.annotation.Nullable;

import hu.szoftverprojekt.holdemfree.R;

public class PlaySound extends Service {

    MediaPlayer player;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        player=MediaPlayer.create(this, R.raw.ss);
        player.setLooping(true);
        player.start();
        player.setVolume(SettingsScreen.volume, SettingsScreen.volume);
        return START_STICKY;
    }

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

Solution

  • Well once you have the SeekBar and the OnSeekBarChangeListener being called as you change the SeekBar, the next major thing is communicating this from your activity to your service this change.

    Have a look at the android docs Creating a bound service for the offical way to communicate between an Activity and Service using no 3rd party libraries.

    HOWEVER

    You could also use a 3rd party library like EventBus (which I'd recommend as it's much easier to use and implement in my opinion).

    Here is an example using EventBus:

    build.gradle:

    dependencies {
        implementation 'org.greenrobot:eventbus:3.2.0'
    }
    

    MediaVolumeEvent.java:

    public class MediaVolumeEvent {
        private int volume;
    
        public MediaVolumeEvent(int volume) {
            this.volume = volume;
        }
    
        public int getVolume() {
            return volume;
        }
    }
    

    PlaySound.java:

    public class PlaySound extends Service {
      @Override
      public void onCreate() {
        super.onCreate();
        EventBus.getDefault().register(this);
      }
    
      @Override
      public void onDestroy() {
        EventBus.getDefault().unregister(this);
        player.stop();
        super.onDestroy();
      }
    
      @Subscribe(threadMode = ThreadMode.MAIN) 
      public void onEvent(MediaVolumeEvent event){
        player.setVolume(event.getVolume(), event.getVolume());
      }
    }
    

    In your activity inside the OnSeekBarChangeListener:

    SeekBar seekBar= (SeekBar) findViewById(R.id.seekBar);
    seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {       
    
        @Override       
        public void onStopTrackingTouch(SeekBar seekBar) {      
            // TODO Auto-generated method stub      
        }       
    
        @Override       
        public void onStartTrackingTouch(SeekBar seekBar) {     
            // TODO Auto-generated method stub      
        }       
    
        @Override       
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            EventBus.getDefault().post(new MediaVolumeEvent(progress));
        }       
    });