Two soundpool volume controls - android

I'm making a white noise app, I would like to control the volume for each sound independently. Is this possible?
Here is my onCreate at the moment.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mySound = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
whiteNoiseId = mySound.load(this, R.raw.whitenoise, 1);
rainId = mySound.load(this, R.raw.rain, 1);
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
SeekBar noiseVolumeControl = (SeekBar) findViewById(R.id.noiseSeekBar);
noiseVolumeControl.setMax(maxVolume);
noiseVolumeControl.setProgress(curVolume);
SeekBar rainVolumeControl = (SeekBar) findViewById(R.id.rainSeekBar);
rainVolumeControl.setMax(maxVolume);
rainVolumeControl.setProgress(curVolume);
noiseVolumeControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
rainVolumeControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
This controls just the main volume, I'd like to be able to lower one and raise the other.
Thanks!

should work with SoundPool.setVolume(int id,float leftVol,float rightVol); method. I have not tried it, but I guess it must be:
mySound.setVolume(whiteNoiseId,0.2,0.2);
mySoundPool.setVolume(rainId,0.8,0.8);

Related

Set volume of a video of a VideoView + SeekBar as control bar

I have to set the volume of a video that is contained in a VideoView. I have to use a SeekBar(I already know how to work with SeekBars, I just would like to know how to set the volume of the video).
I also have to use another SeekBar as controll bar.
This is my code:
positionBar = (SeekBar) this.findViewById(R.id.seekBar2);
volumeBar = (SeekBar) this.findViewById(R.id.seekBar);
//getting the max volume and the current volume to set the volume bar
audioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
maxVolume = audioManager.getStreamMaxVolume(AudioManager.USE_DEFAULT_STREAM_TYPE);
currentVolume = audioManager.getStreamVolume(AudioManager.USE_DEFAULT_STREAM_TYPE);
volumeBar.setMax(maxVolume);
volumeBar.setProgress(currentVolume);
//putting the video to execute in the video view
videoPlayer.setVideoPath("android.resource://" + getPackageName() + "/" + R.raw.video);
//volume bar listener
volumeBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
//********************************************************************
//position bar listener
positionBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
//********************************************************************
//timer to update the position bar during the video execution
//and the volume
new Timer().scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
currentVolume = audioManager.getStreamVolume(AudioManager.USE_DEFAULT_STREAM_TYPE);
volumeBar.setProgress(currentVolume);
}
}, 0 , 500);
//********************************************************************
Check Below Code, To set the volume of the video programatically.
SeekBar volumeBar;
int volumeLevel;
AudioManager audioManager;
ImageView volumnBtn;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
.....
.....
volumeBar = (SeekBar) findViewById(R.id.volumeBar);
audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
volumeBar.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
volumeLevel = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
volumnBtn = (ImageView) findViewById(R.id.volumnBtn);
if (volumeLevel == 0) {
volumnBtn.setImageDrawable(getResources().getDrawable(R.drawable.volumnMute));
} else {
volumnBtn.setImageDrawable(getResources().getDrawable(R.drawable.volumnHigh));
}
iv_volumn_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
volumeLevel = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
volumeBar.setProgress(volumeLevel);
if (volumeBar.getVisibility() == View.GONE) {
volumeBar.setVisibility(View.VISIBLE);
} else {
volumeBar.setVisibility(View.GONE);
}
}
});
volumeBar.setProgress(volumeLevel);
volumeBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
volumeLevel = i;
if (volumeLevel == 0) {
volumnBtn.setImageDrawable(getResources().getDrawable(R.drawable.volumnMute));
} else {
volumnBtn.setImageDrawable(getResources().getDrawable(R.drawable.volumnHigh));
}
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, volumeLevel, 0);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
volumeBar.setVisibility(View.GONE);
if (volumeLevel == 0) {
volumnBtn.setImageDrawable(getResources().getDrawable(R.drawable.volumnMute));
} else {
volumnBtn.setImageDrawable(getResources().getDrawable(R.drawable.volumnHigh));
}
}
});
}
use SeekBar.getProgress() to get value of your seekbar,and then use the value to set volume,in onProgressChanged method.

How to stop a progress of the seekbar only at constant interval

I have a seekbar which ranges from 0 - 100 .I want to create discrete points in the seekbar such that progress stops only at 0,10,20 and so on .I don't to stop in the midddle.
I tried with the below code and I am unable to succeed.
seekBar.setProgress(0);
//seekBar.incrementProgressBy(10);
seekBar.setMax(100);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { progress = progress / 10;
progress = progress * 10;
seekBarValue.setText(String.valueOf(progress));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
int seekBarIntValue;
// seekBar.incrementProgressBy(10);
if((seekBar.getProgress()%10)!=0){
if((seekBar.getProgress()%10)> 5)
{
seekBarIntValue =(seekBar.getProgress()/10)+(seekBar.getProgress()%10);
}else{
seekBarIntValue =(seekBar.getProgress()/10)-(seekBar.getProgress()%10);
}
}else{
seekBarIntValue = (seekBar.getProgress());
}
seekBar.setProgress(seekBarIntValue);
}
});
Please help me out to solve this.
Try this.
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
// round progress up or down to the nearest 10
int roundedProgress = (int) (Math.rint((double) progress / 10) * 10);
if (fromUser)
{
seekBar.setProgress(roundedProgress);
}
}
for that you need to override onProgressChanged method.
i did it in this manner.
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
int interval = 20;
progress = (progress/interval)*interval;
seekBar.setProgress(progress);
}
Try out below code
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
int progress = 0;
#Override
public void onProgressChanged(SeekBar seekBar, int progresValue, boolean fromUser) {
progress = progresValue;
seekBarValue.setText(progress + "/" + seekBar.getMax());
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// Do something here, if you want to do anything at the start of
// touching the seekbar
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// Display the value in textview
}
});
Example force SeekBar stop at 30
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if(progress > 30){
seekBar.setProgress(30);
return;
}
tvProgress.setText(""+progress);
}
public void onStartTrackingTouch(SeekBar seekBar) {
}
public void onStopTrackingTouch(SeekBar seekBar) {
}
});

Increment Seekbar progress by 0.5 android

My seekbar range is 2-15 , i want to increase the progress by 0.5 for each click on the seekbar. I have browsed and could not find a way.
in oncreate()
sbTaux.setProgress(0) //default value.
sbTaux.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
progress = progress / 1;
progress = progress * 1;
if (progress<=2){
progress=2;
sbTaux.setProgress(0);
}
else {}
txTaux.setText(Util.decimalFormat(progress));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
Make range 4-30 and then just divide the result by 2 should do the trick
int myProgress;
SeekBar seekBar = (SeekBar)layout.findViewById(R.id.seekbar);
TextView seekBarValue = (TextView)layout.findViewById(R,id.TextView1);
seekBar.setProgress(2);
seekBar.incrementProgressBy(1/2);
seekBar.setMax(15);
seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
myProgress = progress;
seekBarValue.setText(myProgress);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}

Android: SeekBar volume control in a AsyncTask

I know how to control sound volume with a seekbar:
audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
SeekBar volControl = (SeekBar)findViewById(R.id.volbar);
volControl.setMax(maxVolume);
volControl.setProgress(curVolume);
volControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar arg0) {
}
#Override
public void onStartTrackingTouch(SeekBar arg0) {
}
#Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, arg1, 0);
}
});
but.. I don't know how to do it in a AsyncTask class which show a dialog(with seekbar) while sound is playing... any ideas or examples?
thanks!

create slider to change android volume?

Can someone give me an sample code for changing volume through a slider?
I searched and a lot of tutorials requied me to create a whole new class. Is there an easier way?
Thanks!
Add this to your OnCreate, you have to put your seekbar into the layout xml file:
audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
SeekBar volControl = (SeekBar)findViewById(R.id.volbar);
volControl.setMax(maxVolume);
volControl.setProgress(curVolume);
volControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar arg0) {
}
#Override
public void onStartTrackingTouch(SeekBar arg0) {
}
#Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, arg1, 0);
}
});
Travis at the New Boston has a great Video Tutorial on this here:
http://www.youtube.com/watch?v=8sr2Y6Aff6Y
Source code for the tutorials can be found here: http://www.mybringback.com/bringers/android/thenewboston-android-series/828/thenewboston-sample-projects/
Six years later, this slightly altered version works for me in Android Studio 3.1. The IDE said I had to declare AudioManager final:
/* volume slider*/
final AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
SeekBar volControl = (SeekBar)findViewById(R.id.volControl);
volControl.setMax(maxVolume);
volControl.setProgress(curVolume);
volControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar arg0) {
}
#Override
public void onStartTrackingTouch(SeekBar arg0) {
}
#Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, arg1, 0);
}
});
//end Volume slider

Categories

Resources