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.
Related
Code Run with no errors. But seekbar not working. It is the center but not able to slide on touch.
timesTablesSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
int min =1;
int timesTableNumber;
if(i<1){
timesTableNumber = min;
}else{
timesTableNumber = i;
}
Log.i("Seekbar Value", Integer.toString(timesTableNumber));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
I've just developed a simple media player that sounds a song and it has a seekbar that regulate the song volume.
That's my code:
public class MainActivity extends AppCompatActivity {
Button playBtn;
SeekBar positionBar;
SeekBar volumeBar;
TextView elapsedTimeLabel;
TextView remainingTimeLabel;
MediaPlayer mp;
int totalTime;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
playBtn = (Button) findViewById(R.id.playBtn);
elapsedTimeLabel = (TextView) findViewById(R.id.elapsedTimeLabel);
remainingTimeLabel = (TextView) findViewById(R.id.remainingTimeLabel);
// Media Player
mp = MediaPlayer.create(this, R.raw.song);
mp.setLooping(true);
mp.seekTo(0);
mp.setVolume(0.5f, 0.5f);
totalTime = mp.getDuration();
// Position Bar
positionBar = (SeekBar) findViewById(R.id.positionBar);
positionBar.setMax(totalTime);
positionBar.setOnSeekBarChangeListener(
new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser) {
mp.seekTo(progress);
positionBar.setProgress(progress);
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
);
// Volume Bar
volumeBar = (SeekBar) findViewById(R.id.volumeBar);
volumeBar.setOnSeekBarChangeListener(
new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
float volumeNum = progress / 100f;
mp.setVolume(volumeNum, volumeNum);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
);
// Thread (Update positionBar & timeLabel)
new Thread(new Runnable() {
#Override
public void run() {
while (mp != null) {
try {
Message msg = new Message();
msg.what = mp.getCurrentPosition();
handler.sendMessage(msg);
Thread.sleep(1000);
} catch (InterruptedException e) {}
}
}
}).start();
}
private Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
int currentPosition = msg.what;
// Update positionBar.
positionBar.setProgress(currentPosition);
// Update Labels.
String elapsedTime = createTimeLabel(currentPosition);
elapsedTimeLabel.setText(elapsedTime);
String remainingTime = createTimeLabel(totalTime-currentPosition);
remainingTimeLabel.setText("- " + remainingTime);
}
};
public String createTimeLabel(int time) {
String timeLabel = "";
int min = time / 1000 / 60;
int sec = time / 1000 % 60;
timeLabel = min + ":";
if (sec < 10) timeLabel += "0";
timeLabel += sec;
return timeLabel;
}
public void playBtnClick(View view) {
if (!mp.isPlaying()) {
// Stopping
mp.start();
playBtn.setBackgroundResource(R.drawable.ic_stop);
} else {
// Playing
mp.pause();
playBtn.setBackgroundResource(R.drawable.ic_play);
}
} }
Now, when I regulate the volume with the media player seekbar, the phone system volume didn't change and the other way around.
How can I syncronize the volume seekbar with the phone system volume so as to adjust the volume with both?
Using the Audio Manager to Raise and Down Volume.
AudioManager audioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
//Using volume control UI visibility
//To increase media player volume
audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
//To decrease media player volume
audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
//Without volume control UI
//To increase media player volume
audioManager.adjustVolume(AudioManager.ADJUST_RAISE, AudioManager.FLAG_PLAY_SOUND);
//To decrease media player volume
audioManager.adjustVolume(AudioManager.ADJUST_LOWER, AudioManager.FLAG_PLAY_SOUND);
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);
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) {
}
});
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) {
}