I work with MediaPlayer and want to show ProgressBar before calling method mediaPlayer.prepare() (it can take up some time). I tried AsynkTast, Thread, Handler but it is show progressBar only after all operations at the end of clickPlayButton method. What I do wrong?
Thanks!
ProgressBar declaration:
<ProgressBar
android:id="#+id/progressBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="invisible"/>
clickPlayButton method
public void clickPlayButton(View view) {
Thread t = new Thread(new Runnable() {
public void run() {
h.post(updateProgress);
}
});
t.start();
// progressBar.setVisibility(ProgressBar.VISIBLE);
// new ProgressTask().execute();
mTimer = new Timer();
mMyTimerTask = new MyTimerTask();
if(!mediaPlayer.isPlaying()){
btnPlay.setImageResource(R.drawable.player_stop);
try {
mediaPlayer.setDataSource(DATA_HTTP);
mediaPlayer.prepare();
}
catch (Exception e) { e.printStackTrace(); }
mediaPlayer.start();
mTimer.schedule(mMyTimerTask, 10, 5000);
}
else {
btnPlay.setImageResource(R.drawable.player_play);
mediaPlayer.pause();
mTimer.cancel();
}
// progressBar.setVisibility(ProgressBar.INVISIBLE);
}
Handler h = new Handler();
Runnable updateProgress = new Runnable() {
public void run() {
progressBar.setVisibility(View.VISIBLE);
}
};
Related
I am trying to update seekbar with respect to the progress of the song in MediaPlayer.
I am using Thread to do that task.
First i have used Thred inside thread and trying to update the UI but it crashing the app and says that only original thread can attached to the view.
Then i have try to update it with handler inside the thread runnable. which works fine but it is not updating the seekbar. When i have do log then i come to know loop is not going inside my handler. I dont know where is the problem. Please help me to updating SeekBar.
Code:
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
try {
if ((musicPath != mIRemoteService.getPath())) {
System.out.println("..... MUSIC CHANGE.....");
setOrUpdateData();
updateFragmentLayout();
}
// Displaying Current Duration time/Progress
new Handler().post(new Runnable() {
#Override
public void run() {
try {
songCurrentDurationLabel.setText(""+ Utilities.milliSecondsToTimer(mIRemoteService.position()));
songProgressBar.setProgress((int)mIRemoteService.position());
System.out.println("Runnnnn.........");
//songProgressBar.invalidate();
} catch (RemoteException e) {
e.printStackTrace();
System.out.println("Runnnnn......... EXCEPTION....");
}
}
});
System.out.println("Runnnnn.........MAIN....");
if (!(mIRemoteService.isPlayerRunning())) {
btnPlay.setImageDrawable(getResources().getDrawable(R.drawable.play_now_playing));
mHandler.removeCallbacks(mUpdateTimeTask);
System.out.println("Runnnnn.........MAIN....IF");
}else{
System.out.println("Runnnnn.........MAIN....ELSE");
mHandler.post(mUpdateTimeTask);
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
you can use either a handler or a thread, with thread you should make sure to post the modifications on the UI thread:
private class UpdateSeekBar extends Thread
{
#Override
public void run()
{
super.run();
while (null != mp && mp.isPlaying() && this.isAlive())
{
//final int min = (mp.getCurrentPosition() / 1000) / 60;
//final int sec = (mp.getCurrentPosition() / 1000) % 60;
MainActivity.this.runOnUiThread(new Runnable()
{
#Override
public void run()
{
try
{
songCurrentDurationLabel.setText("" + Utilities.milliSecondsToTimer(mIRemoteService.position()));
songProgressBar.setProgress((int) mIRemoteService.position());
System.out.println("Runnnnn.........");
// songProgressBar.invalidate();
}
catch (RemoteException e)
{
e.printStackTrace();
System.out.println("Runnnnn......... EXCEPTION....");
}
}
});
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
I have Runnable and have two Handlers for button background color change after some music is played.
Now how can I stop the music played using runnable for a ever delay of 700 mili sec on a button click named stop.
handler=new Handler();
runnable = new Runnable() {
#Override
public void run() {
for (int i = 0; i<Uirkeys1.length; i++) {
final int value = i;
try {
Thread.sleep(700);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.post(new Runnable() {
#Override
public void run() {
copyView.get(cnt1).getBackground().setAlpha(100);
ModeSound(Integer.parseInt(CopyMultimap.get(copykey[cnt1]).get(3)));
}
});
try {
Thread.sleep(800);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.post(new Runnable() {
#Override
public void run()
copyView.get(cnt1).getBackground().setAlpha(500);
cnt1++;
}
});
}
}
};
new Thread(runnable).start();
cnt1=0;
}
});
}
You could create a class MyRunnable that implements Runnable, and create a field in it
private boolean stopped = false;
public void stop() { stopped = true; }
And then you should check in your execution loop
if(!stopped) {..do work..} else return .
And you can of course call the stop() method on your MyRunnable object.
I set a textview, which I use for timer-function.
When the timer reachs my wanted time it should start a new activity, that set my Layout to another one.
textfield=(TextView)findViewById(R.id.TVTimer);
handler = new Handler();
Runnable runnable = new Runnable() {
#Override
public void run() {
while (Running){
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
// TODO: handle exception
}
handler.post(new Runnable() {
#Override
public void run(){
number+=1;
textfield.setText(String.valueOf(number));
if (number>5) {
Intent intent = new Intent(getApplicationContext(), Uebung2.class);
startActivity(intent);
}
}
} );
}
}
};
new Thread(runnable).start();
That is the called class:
public class Uebung2 extends Oberklasse {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//Liegestützen1(2);
setContentView(R.layout.new);
}
}
Now I am having the problem after the timer reached the time, which I want, my app is settin g the layout again and again for every second.
What is the solution to set the layout for once?
beacuse you have place this
handler.post(new Runnable() {
#Override
public void run(){
number+=1;
textfield.setText(String.valueOf(number));
if (number>5) {
Intent intent = new Intent(getApplicationContext(), Uebung2.class);
startActivity(intent);
}
}
} );
inside your while loop.
also check your "number" value.
try your while loop as
int i=1000;
while (i<5000){
try {
Thread.sleep(i);
i+=1000;
}
catch (InterruptedException e) {
e.printStackTrace();
// TODO: handle exception
}
}
and after this execute your runnable.
use this
textfield=(TextView)findViewById(R.id.TVTimer);
boolean loop = true;
handler = new Handler();
Runnable runnable = new Runnable() {
#Override
public void run() {
while (loop){
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
// TODO: handle exception
}
handler.post(new Runnable() {
#Override
public void run(){
number+=1;
textfield.setText(String.valueOf(number));
if (number>5) {
loop = false; // this will end loop
Intent intent = new Intent(getApplicationContext(), Uebung2.class);
startActivity(intent);
}
}
} );
}
}
};
new Thread(runnable).start();
I am creating one Progress dialog in my app. Below is my code...
ProgressDialog dialog = ProgressDialog.show(TabViewLayout.this, "", "Logout...");
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.isIndeterminate();
new Thread() {
public void run() {
try {
sleep(5000);
}
catch (Exception e) { }
}
}.start();
Handler handler = new Handler();
handler.post(new Runnable(){
public void run(){
dialog.dismiss();
}
});
What I have to do is, I have to put delay for 5 seconds in my app for some process. With above code, progress dialog is appearing on screen but spinner is not spinning. I want it in spinning mode for 5 seconds. What needs to be done?
Use this code it will work for you
ProgressDialog progressDialog;
Context mContext;
private Thread mThread;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
progressDialog = new ProgressDialog(mContext);
progressDialog.setCancelable(false);
progressDialog.setMessage("Loading....");
progressDialog.show();
mThread = new Thread() {
#Override
public void run() {
try {
synchronized (this) {
// Wait given period of time or exit on touch
wait(5000);
}
} catch (InterruptedException ex) {
}
// finish();
progressDialog.dismiss();
}
};
mThread.start();
}
try this code
ProgressDialog dialog = ProgressDialog.show(this);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.setMessage("Please wait...");
dialog.setCancelable(false);
dialog.show();
new Thread() {
public void run() {
try {
sleep(5000);
}
catch (Exception e) { }
}
}.start();
Handler handler = new Handler();
handler.post(new Runnable() {
public void run() {
dialog.dismiss();
}
});
timer.setText("setTextHere") does not work inside the thread.
Thread thread1 = new Thread(){
TextView timer;
int t;
public void run(){
timer=(TextView) findViewById(R.id.timer);
try{
timer.setText("setTextHere");
sleep(5000);
}
catch(Exception e){
e.printStackTrace();
}
finally{
Intent new1 = new Intent("com.example.app1.MENU");
startActivity(new1);
}
}
};
thread1.start();
_t = new Timer();
_t.scheduleAtFixedRate( new TimerTask() {
#Override
public void run() {
_count++;
runOnUiThread(new Runnable() //run on ui thread
{
public void run()
{
_tv.setText( "" + _count );
}
});
}
}, 1000, 1000 );
You cannot touch UI from background thread. Try to use AsyncTask http://developer.android.com/reference/android/os/AsyncTask.html
On your onCreate() add:
h = new Handler();
add this somewhere:
class SetText implements Runnable {
public void run() {
// do UI task
timer.setText("setTextHere");
}
}
on someFunction() modify like this:
...
try
{
h.post( new SetText() );
sleep(5000);
}
...
final TextView timer = (TextView) findViewById(R.id.timer);
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(2000);
timer.post(new Runnable() {
public void run() {
timer.setText("setTextHere");
}
});
} catch (Exception e) {
e.printStackTrace();
} finally {
Intent new1 = new Intent("com.example.app1.MENU");
startActivity(new1);
}
}
}).start();
you could use this and initialize the setText before initializing the Thread
Thread thread1 = new Thread(){
int t;
public void run(){
try{
sleep(5000);
}
catch(Exception e){
e.printStackTrace();
}
finally{
Intent new1 = new Intent("com.example.app1.MENU");
startActivity(new1);
}
}
};
TextView timer;
timer=(TextView) findViewById(R.id.timer);
timer.setText("setTextHere");
thread1.start();