SeekBar variant that shows waveform data
First of all, include the library in your project.
- Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
- Add the dependency:
implementation 'com.github.alexei-frolo:WaveformSeekBar:1.1'
This section explains how to use WaveformSeekBar.
First, add the view to xml layout:
...
<com.frolo.waveformseekbar.WaveformSeekBar
android:id="@+id/waveform_seek_bar"
android:layout_width="match_parent"
android:layout_gravity="center"
android:layout_height="100dp"
android:layout_margin="16dp"/>
...
Then you can set up waveform data and listen to progress changes as shown below:
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_waveformseekbar_example);
final WaveformSeekBar seekBar = findViewById(R.id.waveform_seek_bar);
seekBar.setOnSeekBarChangeListener(new WaveformSeekBar.Callback() {
@Override
public void onProgressChanged(WaveformSeekBar seekBar, float percent, boolean fromUser) {
}
@Override
public void onStartTrackingTouch(WaveformSeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(WaveformSeekBar seekBar) {
Toast.makeText(
WaveformSeekBarExampleActivity.this,
"Tracked: percent=" + seekBar.getProgressPercent(),
Toast.LENGTH_SHORT).show();
}
});
seekBar.setWaveform(createWaveform(), true);
findViewById(R.id.btn_regenerate).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
seekBar.setWaveform(createWaveform(), true);
}
});
}
private int[] createWaveform() {
final Random random = new Random(System.currentTimeMillis());
final int length = 50;
final int[] values = new int[length];
int maxValue = 0;
for (int i = 0; i < length; i++) {
final int newValue = 5 + random.nextInt(50);
if (newValue > maxValue) {
maxValue = newValue;
}
values[i] = newValue;
}
return values;
}
...