another thing is the timer for seekBar its for the duration of the audio file the issue within it is that the audio file do (mini) stop every 1 sec so how to make seek bar update itself every second without doing any stops
seekBar2.setMax(mediaPlayer.getDuration());
seekBar2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
mediaPlayer.seekTo(progress);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
new Timer().scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
seekBar2.setProgress(mediaPlayer.getCurrentPosition());
}
},0,1000);
public void reset(View v) {
mediaPlayer.reset();
}
Try this to Reset Your mediaPlayer
public void reset(View v) {
mediaPlayer.seekTo(0);
mediaPlayer.start();
}
Try this to update your seekbar
private Handler mHandler = new Handler();
//you have to update Seekbar on UI thread
Your.Activity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
if(mediaPlayer != null){
int mCurrentPosition = mediaPlayer.getCurrentPosition() / 1000;
seekBar2.setProgress(mCurrentPosition);
}
mHandler.postDelayed(this, 1000);
}
});
and whenyou need to update the MediaPlayer's position while user drag your SeekBar use below code
seekBar2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if(mediaPlayer != null && fromUser){
mediaPlayer.seekTo(progress * 1000);
}
}
});
Related
I'm not able to get multiple SeekBar values in multiple TextViews by implementing OnSeekBarChangeListener interface.
By anonymous class I have implemented this but I want this way for my application.
seekBar1.setOnSeekBarChangeListener(this);
}
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
switch (seekBar.getId()){
case R.id.seek1:{
seek1Val=String.valueOf(2000+i*1000);
tvAmount.setText(seek1Val);}
break;
case R.id.seek2:{
seek2Val = String.valueOf(0+i*.5);
tvRate.setText(seek2Val);
}
break;
}
I expect output in each TextView for different SeekBar but value of progress I is taking same for every SeekBar.
better would be to add different setOnSeekBarChangeListener for each SeekBar.
Or if you don't want that then use tag instead id
Best would be:
seekBar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
seek1Val=String.valueOf(2000+i*1000);
tvAmount.setText(seek1Val);}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
seekBar2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
seek2Val = String.valueOf(0+i*.5);
tvRate.setText(seek2Val);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
I need to make multiple seek bar in the same Java class. However, I wonder do we need to set each setOnSeekBarChangeListener for each seek bar? The code seems too long if I am doing so (I need to create 10 seek bar in the same class). Is there any other solution which I can shorten the code? Thank you so much for helping. :)
Below is my code
private SeekBar seekbar,seekbar1;
private TextView a,b;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.schiassess);
seekbar = (SeekBar) findViewById(R.id.delubar);
seekbar1 = (SeekBar) findViewById(R.id.disorbar);
a = findViewById(R.id.a);
b = findViewById(R.id.b);
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
int val = (progress * (seekBar.getWidth() - 2 * seekBar.getThumbOffset())) / seekBar.getMax();
progress++;
a.setText("" + progress);
a.setX(seekBar.getX() + val + seekBar.getThumbOffset() / 2);
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
});
seekbar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
int val = (progress * (seekBar.getWidth() - 2 * seekBar.getThumbOffset())) / seekBar.getMax();
progress++;
b.setText("" + progress);
b.setX(seekBar.getX() + val + seekBar.getThumbOffset() / 2);
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
});
}
Follow the below steps it's simple
JUST IMPLEMENT SeekBar.OnSeekBarChangeListener in your class/fragment
public class Home extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener {
}
You don't have to write new OnSeekBarChangeListener for Every Seekbar...
Just Implement
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
} to your Activity/Fragment..
Now you can check for the id and perform action according to it
I was working in a very simple music player with a seek bar. But I got a problem with playing music,it doesn't work correctly every second - it backwards a while, then remain again like it's a buggy.
I know why but I can't solve it.
When the progress bar moves forward, the music recalibrates and vice versa, when the music moves forward, the progress bar recalibrates.
It's probably because of that that every time it goes back.
But I tried several things and still have the same problem.
Another problem is that when the music is paused, the progress bar goes in the opposite direction until it reaches zero. It doesn't stay where it was before the break.
The full code :
public class MainActivity extends AppCompatActivity {
private MediaPlayer mediaPlayer;
private SeekBar seekBar;
private boolean bool = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.seekBar = (SeekBar) findViewById(R.id.sound_bar);
this.mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.music);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
seekBar.setProgress(mediaPlayer.getCurrentPosition() * seekBar.getMax() / mediaPlayer.getDuration());
}
}, 500, 500);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
mediaPlayer.seekTo(seekBar.getProgress() * mediaPlayer.getDuration() / seekBar.getMax());
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
public void playSound(View view) {
Button button = (Button) view;
if(bool) {
mediaPlayer.pause();
bool = false;
button.setText("Jouer le son");
} else {
mediaPlayer.start();
bool = true;
button.setText("Mettre en pause");
}
}
#Override
protected void onPause() {
super.onPause();
mediaPlayer.pause();
}
#Override
protected void onStart() {
super.onStart();
if(bool) {
mediaPlayer.start();
}
}
}
Maybe use another thread to control the seekbar would help.
Take a look at this
so i want to take progress from seek bar and use it in button method, but its always 0(counter),doesnt work. I tried seekB.getProgress();,seekbar works great...
int counter;
seekB.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
counter = progress;
seekbarValue.setText(String.valueOf(progress));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
//Randomise Button
but.setOnClickListener(new View.OnClickListener() {
int Plength = 6;
char[] i = new char[Plength];
List<Character> pass = new ArrayList<>();
#Override
public void onClick(View v) {
for (int x = 0; x < Plength; x++) {
pass.add(azL[rand.nextInt(azL.length)]);
pass.add(nzL[rand.nextInt(nzL.length)]);
pass.add(AZL[rand.nextInt(AZL.length)]);
i[x] = pass.get(rand.nextInt(pass.size()));
}
textV.setText(String.valueOf(i));
pass.clear();
}
});
if i do textV.setText(String.Valueof(counter)); is going to display 0 all the time.i dont know how to fix it
Try to change it :
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
counter = progress;
seekbarValue.setText(String.valueOf(progress));
}
to
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
counter = progress;
seekbarValue.setText(String.valueOf(new Integer(progress)));
}
Having trouble adding values together from multiple Seekbars. Trying to send the total to a total_view but it keeps showing up as 0. Im using two seekbars and the total should change depending on the movement on the bar so Im using onSeekChangeListener
public class CalculateGradeActivity extends Activity {
TextView text_view; //shows percentage
TextView text_view2; //shows percentage
TextView total_view; //show total from text_view and text_view2
SeekBar seek_bar1;
SeekBar seek_bar2;
int exam_grade;
int homework;
int total;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calculate_grade_view);
seek_bar1 = (SeekBar) findViewById(R.id.seekBarExam);
seek_bar2 = (SeekBar) findViewById(R.id.seekBarHomework);
text_view = (TextView) findViewById(R.id.percentageSeekbar1);
text_view2 = (TextView) findViewById(R.id.percentageSeekbar2);
total_view = (TextView) findViewById(R.id.numeric_grade_id_output);
seekbar();
}
public void seekbar() {
int progress = seek_bar1.getProgress();
text_view.setText(String.valueOf(progress));
int progress2 = seek_bar2.getProgress();
text_view2.setText(String.valueOf(progress2));
total = exam_grade + homework;
seek_bar1.setOnSeekBarChangeListener(
new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
text_view.setText(String.valueOf(progress) + "%");
exam_grade = progress;
total_view.setText(String.valueOf(total));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
);
seek_bar2.setOnSeekBarChangeListener(
new SeekBar.OnSeekBarChangeListener(){
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
text_view2.setText(String.valueOf(progress)+"%");
homework = progress;
total_view.setText(String.valueOf(total));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
);
}
}
Every time you change a value you have to recalculate the total before you display it.
seek_bar1.setOnSeekBarChangeListener(
new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
text_view.setText(String.valueOf(progress) + "%");
exam_grade = progress;
total = exam_grade + homework;
total_view.setText(String.valueOf(total));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
);
seek_bar2.setOnSeekBarChangeListener(
new SeekBar.OnSeekBarChangeListener(){
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
text_view2.setText(String.valueOf(progress)+"%");
homework = progress;
total = exam_grade + homework;
total_view.setText(String.valueOf(total));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
);
ja