#Override
public void run() {
int currentPosition = 0;
int total = mediaPlayer.getDuration();
while (mediaPlayer != null && currentPosition < total) {
try {
Thread.sleep(1000);
currentPosition = mediaPlayer.getCurrentPosition();
} catch (InterruptedException e) {
return;
} catch (Exception e) {
return;
}
pbRingtonePreviewHorizontal.setProgress(currentPosition);
}
}
Above is the code for update progress value, this works perfectly in horizontal progress bar.. I want same for circular progress bar.
Code for initializing progress bar is
public void onPrepared(MediaPlayer mediaplayer) {
System.out.println("on prepared");
if (!mediaPlayer.isPlaying()) {
isPlayEnabled = true;
mediaPlayer.start();
pbRingtonePreview.setVisibility(View.INVISIBLE);
pbRingtonePreviewHorizontal.setProgress(0);
pbRingtonePreviewHorizontal.setMax(mediaPlayer
.getDuration());
ivRingtonePreview.setImageResource(R.drawable.stop);
new Thread(this).start();
}
}
Related
I am using IVideon Player and I have an URL which I want to play. Things are okay but I am unable to handle Seekbar status. I am getting 0 when I want to get time how much my video has been played. Please help me out. What I did is here
class SeekBarProgress implements Runnable {
#Override
public void run() {
int totalDuration = event_list.get(counter).getDuration();
for (int i = 0; i <= totalDuration; i++) {
if (isPlaying) {
final int value = i;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
mTimeUpdateHandler.post(new Runnable() {
#Override
public void run() {
strttime.setText(Utils.getDuration(Utils.getLong((double) value))); // the TextView Reference
mBufferBar.setProgress(value);
}
});
}
}
}
}
I'm facing with a problem. I want to get duration of video when i 'm recording it.
I can get duration of video when it's finish by code
MediaPlayer mp = MediaPlayer.create(mContext, Uri.parse(video));
if(mp == null)
return -1;
int duration = mp.getDuration();
mp.release();
But i want to get duration everytime when i record video to update to progressbar.
Hope your reply!
private class PlaybackObserver extends Thread {
public void run() {
currentPosition = 0;
try {
while (!killObserverThread) {
Thread.sleep(1000);
currentPosition = (int) mediaPlayer.getCurrentPosition();
runOnUiThread(new Runnable() {
public void run() {
yourProgressBar.setProgress(currentPosition);
}
});
}
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
#Override
protected void onDestroy() {
super.onDestroy();
killObserverThread = false;
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
if (task == null || !task.isAlive()) {
task = new PlaybackObserver();
task.start();
}
startPlaying();
}
add a private class for your UiThread to update your seekbar/progressbar. Then start it from onResume()
I want to play a music so I used seekber to show the progress, everything is fine until I press pause button, the seekbar stops but when I press the play button the seekbar does not move, I figured out it is because of getcurrentposition() method, it does not update after pausing, below is my code, what's wrong with it?
#Override
public void onClick(View v) {
try {
if (isPlaying) {
buttonPlayPause.setImageResource(R.drawable.play);
mediaPlayer.pause();
isPlaying = false;
} else {
buttonPlayPause.setImageResource(R.drawable.pause);
isPlaying = true;
mediaPlayer.start();
primarySeekBarProgressUpdater();
}
} catch (Exception e) {
onDestroy();
}
}
});
private void primarySeekBarProgressUpdater() {
try {
seekBarProgress
.setProgress((int) (((float) mediaPlayer
.getCurrentPosition() / mediaFileLengthInMilliseconds) * 100));
if (mediaPlayer.isPlaying()) {
Runnable notification = new Runnable() {
public void run() {
primarySeekBarProgressUpdater();
}
};
handler.postDelayed(notification, 1000);
}
} catch (Exception e) {
onDestroy();
}
}
i have a seekbar that should move while streaming from url but it's not automatically moving, but when you press on the seekbar it takes you where you should be. and my class doesnt fire run() function!
public void run() {
// TODO Auto-generated method stub
int currentPosition = 0;
int total = mediaPlayer.getDuration();
seekprogress.setMax(total);
while (mediaPlayer != null && currentPosition <total) {
try {
Thread.sleep(100);
currentPosition = mediaPlayer.getCurrentPosition();
} catch (InterruptedException e) {
return;
} catch (Exception e) {
return;
}
seekprogress.setProgress(currentPosition);
}
}
You can update UI elements only from the UI thread. Use runOnUiThread
in my app i am using media player and i want to place a seek bar there. In my code the song is playing but the seek bar is not moving. Following is the part of my code...
sv = (SurfaceView)findViewById(R.id.surfaceView1);
sv.postDelayed(runnable,200);
final Runnable runnable = new Runnable()
{
#Override
public void run()
{
if (mediaPlayer != null)
{
if (isPlaying)
{
try
{
int currentPosition = mediaPlayer.getCurrentPosition();
audioProgress.setProgress((int) ((currentPosition / (float) mediaPlayer.getDuration()) * 100f));
} catch (Exception e) {
e.printStackTrace();
}
sv.postDelayed(runnable, 150);
}
}
}
};
Please anyone help me in clearing the errors...
Make these changes to your code:
sv = (SurfaceView)findViewById(R.id.surfaceView1);
audioProgress.setMax(mediaPlayer.getDuration()); // this is one change
sv.postDelayed(runnable,200);
final Runnable runnable = new Runnable()
{
#Override
public void run()
{
if (mediaPlayer != null)
{
if (isPlaying)
{
try
{
int currentPosition = mediaPlayer.getCurrentPosition();
// the other change:
audioProgress.setProgress(currentPosition);
} catch (Exception e) {
e.printStackTrace();
}
sv.postDelayed(runnable, 150);
}
}
}
};