Method onStartTrackingTouch and getProgress() of seekbar. First value is always 0. - java

For a lock/unlock system I use the onStartTrackingTouch and onStopTrackingTouch methods. And testing, I don't understand one thing:
public void onStartTrackingTouch(SeekBar seekBar) {
Log.d(TAG,"Progress: "+seekBar.getProgress());
}
The first value is always zero.
Why? I need the real value!
Can anybody help me?
Thanks in advance.

I've changed my idea, finally i've used onProgressChanged:
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
if(progress>95){
seekBar.setVisibility(SeekBar.INVISIBLE);
//code
}
public void onStopTrackingTouch(SeekBar seekBar) {
seekBar.setProgress(0);
}
Thanks

Related

Glitches while playing audio in Android

I am using MediaPlayer to play audio in Android. MediaPlayer is working perfectly but when I use a SeekBar which is updated with the duration of audio, the audio doesn't play smoothly and breaks while playing. Below is my code snippet for reference.
mSongSeekBar.setMax(mMediaPlayer.getDuration());
mSongSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
mMediaPlayer.seekTo(progress);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
new Timer().scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
mSongSeekBar.setProgress(mMediaPlayer.getCurrentPosition());
}
}, 0, 500);
What is the reason for this behavior? How to get rid of this? Should I go with any alternative of Timer?
I had the same problem where the audio was continuously seeking by itself and causing glitches. You need to make sure the audio is seeked only when the user scrubs it and not when it is normally running. For this purpose the boolean fromUser parameter of onProgressChanged is useful. This would ensure the audio seeking only when the user scrubs it.
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if(fromUser){
mMediaPlayer.seekTo(progress);
}
}
Reason
Audio is not playing continuously, it is changing every 500 millisecond, and so it feels breaking. Because Timer() is changing the value of Progress integer. And mMediaPlayer seeks to it every time automatically, as the code is written:
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
mMediaPlayer.seekTo(progress);
}
How to Get rid of this
You should use boolean fromUser to change position of audio, only when user changes seekBar manually, You can do this by just a little change(using if)
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser)
mMediaPlayer.seekTo(progress);
}
You might consider overriding the onStartTrackingTouch and onStopTrackingTouch methods as well. Because when you are dragging the seek-bar the onProgressChanged is called frequently and each time it is trying to set the media player to play from a specific position and hence you are getting that cracking or breaking of your audio.
I would recommend, pausing the media player temporarily while the seek-bar being dragged and resume the audio when you are done with the dragging. The methods mentioned above might help you to achieve those behaviors.

Seekbar in Android Studio

I came across a piece of code,now I am stuck with it.
SeekBar volumeControl=(SeekBar)findViewById(R.id.volumeSeekBar);
volumeControl.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) {
}
});
Here I know that volumeControl is a variable of type SeekBar. In the second line of code volume control is set with a function setOnSeekBarChangeListener. I am unable to understand what's written inside the brackets of setOnSeekBarChangeListener. Can anyone please explain it in detail. I am just introduced to java and don't have much knowledge
This is a small piece of code to control volume using a seek bar.
Inside the brackets of onSeekBarChangeListener, we declare a new SeekBar.onSeekBarChangeListener which implements three methods :
onProgressChanged : This basically tracks the change in the seek bar and then sets the volume according to the amount of change.
onStartTrackingTouch : This methods contains the code which should be executed when the touch gesture starts.
onStopTrackingTouch:
This method contains the code which should be executed which the touch gesture stops.

Equalizer UnsupportedOperationException: AudioEffect: invalid parameter operation

This is my equalizer class, and for a long time it worked without any problem,but now i'm getting this error.
P.S yesterday i flashed a custom rom on my phone, could that be the problem?
Because on another device it works fine.
Error
Caused by: java.lang.UnsupportedOperationException: AudioEffect: invalid parameter operation
at android.media.audiofx.AudioEffect.checkStatus(AudioEffect.java:1299)
at android.media.audiofx.Equalizer.setBandLevel(Equalizer.java:223)
My code
verticalSeekbar[i].setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {
short newLevel = (short)(progress + lowerEqualizerBandLevel);
short band = (short)seekBar.getTag();
bandLevels[band] = newLevel;
if (mEqualizer != null) {
mEqualizer.setBandLevel(band, newLevel);
}
slider_value[band].setText((progress + lowerEqualizerBandLevel) / 100 + " dB");
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
slider_labels[i].setText(formatBandLabel(freq_range));
}
EDIT
Im certain that the problem is the custom rom because on stock roms it works fine.
What could be the reason it throws UnsupportedOperationException on custom roms?

Updating Seekbar after changing Edittext

So I've got a seekbar and an edittext. My aim is to type in a number in the edittext, and in the same time the seekbar should jump to the related position. This should also work vise versa.
I've already got the code for them seperately. But something is missing. Because when I put in both and start then i can slide the seekbar and the value is changing in the same time in the edittext. But when i try to type in a number, it jumps to the beginning. Example: i want to type 123...result is 321. more or less the same with deleting.
I think the two codes are crashing each other, bit i don't know what to change.
And maybe i should say that i'm new in this field.
I'm not sure if my intentions are understandable..
Here my codes:
1.
value.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int after, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
try{
//Update Seekbar value after entering a number
int progress = Math.round(Float.parseFloat(s.toString()));
seekbar.setProgress(progress);
value.setText(value.getText().length());
} catch(Exception ex) {}
}
});
2.
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
value.setText(String.valueOf(progress));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
As you see, there is a parameter passed into "onProgressChanged" function called "fromUser":
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
It defines that progress is changed by "physically" shfting the slider, so all You have to do, is just type as below:
if (fromUser) value.setText(String.valueOf(progress));
It prevents doing this kind of loop.
value.setText(value.getText().length());
This line is called first, changing the text.
value.setText(String.valueOf(progress));
Then this line is called, changing the text to something else. These 2 setText()'s happen so fast, you cannot see that it happened twice, you only see the result of the last one.
I propose you remove the value.setText(value.getText().length());, displaying the length of the value string is not something you want to do, if I understand your question correctly.
The problem is in your afterTextChanged() method.
1. Remove line value.setText(value.getText().length()); from afterTextChanged().
value.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence charSequence, int after, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
try {
int progress = Math.round(Float.parseFloat(s.toString()));
seekBar.setProgress(progress);
} catch (Exception e) {}
}
});
2. Update onProgressChanged() method as below, to move cursor position as per text change.
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
value.setText(String.valueOf(progress));
value.setSelection(String.valueOf(progress).length());
}
OUTPUT:
Hope this will help~
value.setText(value.getText().length()); is not using to display the length. It is used to place the cursor position at the end of edittext instead of going it to start position

saving the state of a SeekBar

I'm struggling to find the best way to implement my update to the database
The code below is in the adapter for a list view, so each list item has its own seekbar.
The list is of homework assignments so each item has its own degree of completion which I need to persist to a database.
I have a business method which can do the database write, it just needs the id of the assignment and the new progress value.
private void attachProgressUpdatedListener(SeekBar seekBar) {
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar seekBar) {
// need to update database with new progress here!
}
public void onStartTrackingTouch(SeekBar seekBar) {
// empty as onStartTrackingTouch listener not being used in
// current implementation
}
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// empty as onProgressChanged listener not being used in current
// implementation
}
});
}
the problem I'm having is getting a handle to my AssignmentHandler which deals with the database reads / writes. I can pass the assignment_id to my attachProgressUpdatedListener without any issues but I'm not sure this is the best way to go about it as I haven't got / not sure I want an instance of AssignmentHandler instantiated inside the ArrayAdapter.
You can store the element's database ID in each SeekBar's "tag" (setTag() and getTag() methods), and use that to update the database from the onStopTracking method.

Categories

Resources