My aim is to manage volume by seekbar of background music. I created background music by service. I have media player in Service class:
public class ModernService extends Service {
private MediaPlayer modernsong;
private SeekBar volumeSeekBar;
AudioManager am;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
public ModernService() {
super();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
modernsong =MediaPlayer.create(this,R.raw.modern);
modernsong.setLooping(true);
modernsong.start();
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
modernsong.stop();
}
}
The question is how can i get float from a seekbar that is on layout. Thanks in advance
First initialize the volumeSeekbar
and set the max value.
volumeSeekBar = findViewById(R.id.seekBarId); volumeSeekBar.setMax(100);
Then you need to add OnSeekBarChangeListener
to the veiw.
volumeSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
//any ui changes you wanted to do on user action on seekbar
}
});
volumeSeekBar.getProgress();
In service class onStartCommand
is having direct access to intent.
So while starting the service through intent you can pass the seekbar value.
Intent mIntent = new Intent(this, Service.class);
Bundle extras = mIntent.getExtras();
extras.putString(key, seekbarValue);