I've made an Android app. In this app there is a sound player. I've put a Seekbar for audio player. However, when playing audio, the sound is on and off frequently. This problem has been created since adding seekbar.I use Thread for solve this problem but it not solved.I use this codes in OnCreate method :
btn_playSound.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
stop_background_sound();
if (mPlayer.isPlaying()) {
mPlayer.pause();
} else {
mPlayer.start();
new Thread() {
public void run() {
seekBar.post(new Runnable() {
#Override
public void run() {
// seekTo(getCurrentPosition());
seekBar.setProgress(mPlayer.getCurrentPosition());
seekBar.setMax(mPlayer.getDuration());
mHandler.postDelayed(this, 100);
}
});
}
}.start();
}
call_audio_player();
}
});
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
seekTo(progress);
}
});
As I expected, you are updating progress of player's playback even when you change the position of seekbar with your handler, not changed by user.
replace
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
seekTo(progress);
}
with
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if(fromUser) {
seekTo(progress);
}
}
Related
When I add the seekTo function inside onProgressChanged a voice interrupt happens after every seekbar jump. How can I prevent this?
final SeekBar seekbar2 = findViewById(R.id.ScrubSeekBar);
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);
}
I need to run a method every time the position of my seekbar is changed.
It does not matter:
how far it has moved
the direction
if its at the end or the start.
just that it has changed
is there a equivalent to onclickListener() for a seekbar.
Use this method provided by SeekBar class:
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//progress changed
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
The seekbar has a setOnSeekBarChangeListener. Check this for more details https://developer.android.com/reference/android/widget/SeekBar.OnSeekBarChangeListener.html
seekBar.setOnSeekBarChangeListener(
new OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar,
int progresValue, boolean fromUser) {
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
i want to convert write onprogress function into stop. means when is release seekbar then volume is up and down.
sB.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i/*progress*/, boolean b) {
aM.setStreamVolume(AudioManager.STREAM_MUSIC,i,0);
// Log.i("Progress","Called"+i);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
Log.i("Start","Called");
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
Log.i("Stop","Called");
}
});
Do like this::
private int volumLevel = 0;
sB.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i/*progress*/, boolean b) {
if(b){
volumLevel = i;
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
Log.i("Start","Called");
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
aM.setStreamVolume(AudioManager.STREAM_MUSIC,volumnLevel,0);
Log.i("Stop","Called");
}
});
asper our knowledge onStopTrackingTouch event is called after releasing seekbar, so you can update your volume when called.
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
//update your volume using
seekBar.getProgress();
}
I have an Activity (let call it A) and service (let call it B) with music player in it.
In A there's a seekbar, and I want to change MediaPlayer's volume with it.
Any ideas?
In MainActivity:
MusicService mms;
mms = new MusicService();
Settings_Sound_Music_Volume.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
sdm.setSetting("SETTINGS_MUSIC_VOLUME",String.valueOf(Settings_Sound_Music_Volume.getProgress()));
mms.player.setVolume(Settings_Sound_Music_Volume.getProgress(), Settings_Sound_Music_Volume.getProgress());
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
// TODO Auto-generated method stub
Settings_Sound_Music_VolumeValue.setText(String.valueOf(progress) + "%");
}
});
But this code crashes my app. I get a NullPointerException at this line: mms.player.setVolume(...);
i m using seekbar in a game in which i want it to set to zero location by default when user leave it
verticalSeekBar=(VerticalSeekBar)findViewById(R.id.vertical_Seekbar);
vsProgress=(TextView)findViewById(R.id.vertical_sb_progresstext);
tv1=(TextView)findViewById(R.id.text1);
verticalSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
setProgress(0);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
vsProgress.setText(progress+"");
if(progress==10)
{
check();
}
}
Use
VerticalSeekBar.setProgress(0);
instead of
seekBar.setProgress(0);