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);
}
}
}
};
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 have made an android app in that i have implemented play pause function,But I only knows about play ,I don't know how to stop sliding on the same button click,I have used two functions for it as below,play is working but pause is not.!
onClick
if (!flag) {
play();
new Thread(ViewPagerVisibleScroll).start();
ivPlay.setBackgroundResource(R.drawable.pause);
flag = true;
} else {
pause();
new Thread(ViewPagerVisibleScroll).start();
ivPlay.setBackgroundResource(R.drawable.play_btn);
flag = false;
}
play and pause
void play() {
pos = viewPager.getCurrentItem();
ViewPagerVisibleScroll = new Runnable() {
#Override
public void run() {
try {
if (pos <= adapter.getCount() - 1) {
viewPager.setCurrentItem(pos, true);
viewPager.setScrollDurationFactor(6);
handler.postDelayed(this, 3000);
pos++;
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
}
void pause() {
pos = viewPager.getCurrentItem();
ViewPagerVisibleScroll = new Runnable() {
#Override
public void run() {
try {
if (pos <= adapter.getCount() - 1) {
viewPager.setCurrentItem(pos, true);
viewPager.setScrollDurationFactor(6);
handler.postDelayed(this, 300000);
pos++;
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
}
in pause button click :
handler.removeCallbacksAndMessages(ViewPagerVisibleScroll);
Try this
#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();
}
}
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 have a method which I am calling from onCreate( main thread). Inside the method I am calling one more method and a Runnable that calls the same method repeatedly. This is how it looks,
public void start() {
try {
if (start == 0) {
playpause.setBackgroundDrawable(getResources().getDrawable(
R.drawable.play));
initialPoiPlayStatus = true;
playAudio();
}
if (play) {
if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
// for displaying view stub
poi = listOfPOI.get(j);
int pos = mMediaPlayer.getCurrentPosition();
int convSeconds = -1;
convSeconds = ((int) (pos) / 1000);
// displaying the you tube pop up based on the parsed value
// from the plist
youTube(convSeconds);
// call the method to change the image while the audio is
// playing
changeImage();
seek_bar.setProgress(current_position);
notification = new Runnable() {
public void run() {
start();
}
};
handler.postDelayed(notification, 1000);
} else {
if (j != (listOfPOI.size() - 1)) {
firstPlay = true;
initialPoiPlayStatus = true;
j++;
playAudio();
start();
// retrieve mascot info
if (isNetworkAvailable() == true) {
receiveResponse();
} else {
}
} else {
threadHandler.sendEmptyMessage(0);
}
}
}
} catch (Exception e) {
System.out.println(poi.getPlistPath());
e.printStackTrace();
}
}
Now inside youtube,
private void youTube(int convSeconds) {
frameLayout = (FrameLayout) findViewById(R.id.frameLayout);
youtubeWebView = (WebView) findViewById(R.id.youtubewebView);
try {
if (poi.getIsYoutubePresent().equals("true")) {
//mMediaPlayer.pause();
isNetworkAvailable();
if (isNetworkAvailable() == true) {
String startTimeyoutube = poi.getYoutubestartTime();
Integer youTubeStartTime = Integer
.parseInt(startTimeyoutube);
if (convSeconds == youTubeStartTime) {
if (frameLayout.getVisibility() == View.GONE) {
frameLayout.setVisibility(View.VISIBLE);
slideUp = AnimationUtils.loadAnimation(this,
R.anim.slide_up);
frameLayout.startAnimation(slideUp);
}
final String youTubeurl = poi.getLink();
WebSettings websets = youtubeWebView.getSettings();
websets.setJavaScriptEnabled(true);
registerForContextMenu(youtubeWebView);
youtubeWebView
.setWebViewClient(new ItemsWebViewClient());
youtubeWebView.loadUrl(youTubeurl);
youtubeWebView.getSettings().setLayoutAlgorithm(
LayoutAlgorithm.SINGLE_COLUMN);
firstPlay = false;
initialPoiPlayStatus = false;
paused = true;
play = false;
playpause.setBackgroundDrawable(getResources().getDrawable(
R.drawable.play));
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.pause();
}
Button goBtn = (Button) findViewById(R.id.youtubeButton);
goBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View V) {
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse(youTubeurl)));
}
});
ImageButton close = (ImageButton) findViewById(R.id.youtubecloseButton);
close.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
frameLayout.setVisibility(View.GONE);
if (!mMediaPlayer.isPlaying()) {
mMediaPlayer.start();
}
playpause.setBackgroundDrawable(getResources().getDrawable(
R.drawable.pause));
}
});
}
} else {
Toast.makeText(getApplicationContext(), "No network available", Toast.LENGTH_SHORT).show();
}
} else {
frameLayout.setVisibility(View.INVISIBLE);
}
} catch (Exception e) {
e.printStackTrace();
}
return;
}
Here whenever, if (convSeconds == youTubeStartTime) {} becomes true, I am making my audio pause i.e.,
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.pause();
}
And during this time also I want the start() inside Runnable notification should be keep calling. But if I make audio pause this is not happening only. The control is completely lost in this case. And on pressing the Image button close, i am trying to make the audio again to play like this
if (!mMediaPlayer.isPlaying()) {
mMediaPlayer.start();
}
Audio keeps playing now, but the slider/seekbar is not moving because start() inside notification is not getting called.
Do any one know why is it happening like this?
Note: How ever if I don make my mediaplayer not to pause, it works fine. I get the problem onky when I make the audio to pause
I solved the issue finally, it may not be the standard way. But it is working for me.
i just replicated
notification = new Runnable() {
public void run() {
start();
}
};
handler.postDelayed(notification, 1000);
where I am trying to close the image button, i.e.,
ImageButton close = (ImageButton) findViewById(R.id.youtubecloseButton);
close.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
frameLayout.setVisibility(View.GONE);
if (!mMediaPlayer.isPlaying()) {
mMediaPlayer.start();
}
notification = new Runnable() {
public void run() {
start();
}
};
handler.postDelayed(notification, 1000);
}
});