MediaPlayer stops audio sound - android

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);
}

Related

running a method when a seekbar changes in any way

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) {
}
});

Implementation of seekbar start code function write in stop fuction in media player

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();
}

How to simultaneously change the value of one seekbar based on other

Have three seekbars on one Activity. All of them have max of 100. I need to sum of all three was 100.
When seek1=50 and seek2=45 then seek3 must be 5.
When I slide seek1 to 55 seek3 must have 0. etc.
How can I code this?
Sorry, here is my code:
//01
final SeekBar seekBar01 = (SeekBar)findViewById(R.id.seek01);
final TextView seekBar01Value = (TextView)findViewById(R.id.txt01val);
seekBar01.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
seekBar01Value.setText(String.valueOf(progress));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
//02
SeekBar seekBar02 = (SeekBar)findViewById(R.id.seek02);
final TextView seekBar02Value = (TextView)findViewById(R.id.txt02val);
seekBar02.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
seekBar02Value.setText(String.valueOf(progress));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
//03
SeekBar seekBar03 = (SeekBar)findViewById(R.id.seek03);
final TextView seekBar03Value = (TextView)findViewById(R.id.txt03val);
seekBar03.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
seekBar03Value.setText(String.valueOf(progress));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
I can show values on respective TextViews, but don't get how to change another seebar progress from other seekbar onProgressChanged.
Set OnSeekBarChangeListener to your seekbars. When one changes, set the values to the others with setProgress
Trouble was because of late declarations of SeekBar's.
If gather all SeekBar seekBarXX = (SeekBar)findViewById(R.id.seekXX); sooner than Change listeners all works as expected.
Sorry for stupidity.
Here is code:
final SeekBar seekBar01 = (SeekBar)findViewById(R.id.seek01);
final SeekBar seekBar02 = (SeekBar)findViewById(R.id.seek02);
final SeekBar seekBar03 = (SeekBar)findViewById(R.id.seek03);
seekBar01.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
seekBar02.setProgress(100-seekBar01.getProgress());
}
...
});

why sound on and off frequently after add seekbar to audio player?

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);
}
}

two different seekbars with different tasks

I have one SeekBar which does specific task. I want to add another SeekBar with different task.
How can I put
seekbar.setOnSeekBarChangeListener(this);
unique for each SeekBar. Now when I touch either seekbar it results same.
You have a couple options:
1) The common practice is to use an anonymous class. Like this:
curSlider.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
Log.d("slider","stop tracking");
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
}
});
2) If you want to use this, you need to use some logic to figure out which SeekBar is being moved (using ids or object references). Like:
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
if (seekBar == mySeekBar1) {
} else if (seekBar == mySeekBar2) {
}
}
you can add any no. of seekbars and every one to do something
I have a sample code with 3 seekbars every one send its progress value and all together create a colore to set as a background to imageview
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.SeekBar;
public class MainActivity extends Activity {
SeekBar seekBar1, seekBar2, seekBar3;
ImageView imageView;
public int pro1,pro2,pro3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) this.findViewById(R.id.imageView);
seekBar1 = (SeekBar) this.findViewById(R.id.seekBar1);
seekBar2 = (SeekBar) this.findViewById(R.id.seekBar2);
seekBar3 = (SeekBar) this.findViewById(R.id.seekBar3);
seekBar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
pro1= progress;
imageView.setBackgroundColor(Color.argb(200, pro1, pro2, pro3));
}
#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) {
pro2= progress;
imageView.setBackgroundColor(Color.argb(200, pro1, pro2, pro3));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
seekBar3.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
pro3=progress;
imageView.setBackgroundColor(Color.argb(200, pro1, pro2, pro3));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
}
this youtube
https://youtu.be/AM6lJnze9nA

Categories

Resources