i have to display text of the playing audio (file in sdcard). After given audio progress time i have to change the text of my TextView.
For this i think i should make a thread which monitor this stuff and behave like following:
if((player.isPlaying) == 0)
{
get new text and set to textView
pick relavent audio and play
}
can we associate a thread or some function like that to our Activity that constantly monitors it
I am sorry i am new to android please help me with some example or at least idea!!!
First, there is no such thing as "associate a thread with Activity" in Android. If you want to manage thread in parallel with Activity lifecycle - you can do that. You'll need to start when you want it, and stop it in onDestroy() (to put it briefly).
Also, in your particular case you do not need to have separate thread that monitors player. You need to set different listeners for you player. Assuming you're using standard Android's MediaPlayer, you need to set: OnErrorListener and OnCompletionListener.
This will allow you to not waste resources and have precise reaction on player's state changes.
Related
I'm trying to change the imageview 3 times in a short period of time.
I want to set the image, have sheldon say "Bazinga", then change imageview directly after he the audio is completed. And then I want to do that a second time.
Note that the first image is already set before calling this function:
sound = MediaPlayer.create(this, R.raw.bazinga);
sound.start();
page.setImageResource(R.drawable.counttotwofirst);
while(sound.isPlaying()){}
sound.start();
page.setImageResource(R.drawable.counttotwosecond);
while(sound.isPlaying()){}
sound.release();
page.setClickable(true);
I've tried to use invalidate(); but that didn't work.
Is there anyway to force update or at least wait until the imageview has been updated?
How long time is there between updates if imageviews?
Is there some other view I should use instead?
You need to re-think the design a bit. I am guessing you are running all of that code on the main (UI) thread of your app. The problem is that although you are changing the image resource, you are then causing the UI thread to block (sitting in a while() loop), so the UI updates don't get performed until after your entire function returns. You probably need some more pieces to achieve the sound/animation effects you want. Off the top of my head, probably create a background thread for performing the sound loading/playback so that's not done on the UI thread. Also, instead of sitting in a while() loop until the sound finishes, it's better to have a callback notify you that playback has completed (take a look at MediaPlayer::setOnCompletionListener()).
I'm trying to create a turn based game using a 1v1 battle for android. My basic game loop checks if the two fighters are dead, if not then checks who is to go next. If its the player's turn then it should wait for an attack button to be clicked. If its the computer's turn, then it will execute a random attack. I'm having trouble getting the program to wait for the user input. I tried setting the button listener here but that's not doing it.
[edit] The determination for which character goes is based on a recovery integer. Each attack has a recovery value (50-100) which is added to the character's recovery. The nextMove() method checks to see which is closer to 0 and subtracts the difference from both characters. This allows the game to require more strategy because you don't attack just once a turn.
What can I do to get the game to pause at that point
Here's the code
public void battle(){
boolean playerGo;
while(!checkDead()){
playerGo=nextMove(); //returns true if its the players turn to go
if(playerGo){
//The game should wait here for the user input
moveButton1.setOnClickListener(this);
}
else{
randomMove(); //game automatically goes
}
}
}
When your app starts up, there's one thread on which everything runs, including event handlers. After you do your setup and call battle(), that thread is sitting there going around and around the loop. It's so busy going around and around the loop that it doesn't notice that there's a click event waiting to be processed!
There's a few options:
Restructure your code. It looks like the basic structure is that the player moves, then the game moves. You could remove this loop entirely, and instead call randomMove() after each time you handle the player's move. Handle the player's move in the OnClickListener for moveButton1. That way everything just happens on events. This would be simpler overall, and is probably the Right Thing to do.
Make the smallest possible change to your code to get it working. This would probably mean pulling the contents of your while loop into a Runnable, which you schedule by calling Handler.post. The first line calls checkDead and returns if true. The last line reschedules the Runnable. In between is the body of the while loop. The effect of this is that your loop body runs, then the event handler gets a turn, then your loop body runs, then the event handler runs. This is probably a bad idea.
Run battle() in another thread. This is probably a bad idea.
Why are 2. and 3. bad ideas? On a mobile device, battery life is precious, and running a check to see if you need to do something over and over again will keep the CPU busy chewing up battery life. Much better to sit there idle until you need to do something - this is what option 1 achieves.
So if 2. and 3. are bad ideas, why mention them? Welllllll, 2. I mention because it's the closest thing I've got to an answer to the question you actually asked. I mention 3. because there's a sense in which your current code is a fairly clear embodiment of the game logic. You could rework it so it runs in a separate thread, and instead of nextMove() returning true, nextMove() waits until the player makes a move (this would involve semaphores or mutexes or promises). But this would be an explicitly multi-threaded program, and as such would be difficult to write correctly. I recommend you don't attempt it at this stage in your programming career - the most likely outcome is a program that stops and waits forever, or that corrupts its data structures, in a way that is exceedingly difficult to diagnose.
Button.SetOnClickListener() function will be triggered, only when the user clicks on the button. As such it doesn't wait\block till the user input. This is by design in Android, that you cannot have a blocking window waiting for user input. Instead change your design to display hint saying 'now its user's move'.
User does first move by clicking the button.
SetOnclickListener() will be invoked. Have the user action code inside it.
Towards end of SetOnclickListener() have the computer action code.
With this cycle you can have user move and computer move chained.
in my app, i'm uploading some files that can take up to several minutes. i'm thinking of a way to notify the user about activity going on passively by adding a progress bar in my custom title bar. what i want to do is have every activity, each which uses the custom titles, appear with the progress bar until the thread finishes and does a callback which would make invisible the progress bar. can something like this be accomplished?
what seems to make this impossible is that if the user is in an activity with the view loaded, the thread finishing callback would have to manipulate the loaded view resources to disable the progess bar which doesn't seem feasible. are there any suggestions to accomplish this or alternative solutions in keeping a global and passive indication of something going in the background?
You can use a service to achieve this. Services
Basically how it would work, is you bind to the service in each activity when you create the activity. You use this service to start your upload method.
When you bind to the service you pass a handler, which is then used to update your UI in that specific activity. The service will never directly affect the UI (it will be running on a separate thread) instead the handler passes a message back to the UI thread with data in a Bundle, such as upload progress, or a bool to say it's finished.
hey guys
I am trying to make a media player in which there is a seekbar.
Seekbar is running with a thread ; this thread is a runnable and not another class.
So far i have added the seekbar and now i want to stop the seekbar when pause button is pressed but there is nothing like :-
myThread.stop or myThread.pause??
Also i have to run one thread in background since if the user comes back to "now Playing"
activity he can see the seekbar at correct position
Thread is holding me back
Thanks
pause and stop and all those other methods don't exist any more (for very good reasons).
what you want to do is let your thread sit in a loop and do whatever it is you want it to do. Put in some settable flags that you can control from outside that will let the thread exit when you're done with it.
Keep in mind that threads aren't re-usable. Once you have run a thread you can't run it again.
Also, the seek bar can be set to the correct position when the user returns to your activity (I assume you'll keep the music playing in the background). There's no need to waste resources updating the seekbar's position while the activity is not in display.
What about using the buffering update callback provided by MediaPlayer?
It doesn't seem reasonable to me to have a thread running in the background to do this.
I have a main game thread but when the target score is achieved I have an activity that is launched called StageCleared which basically displays the stats to the user of their performance and then they can press a continue button to carry on with the game. This should switch focus back to the running thread that should continue execution, and thus display the game activity (with parameters i update after StageCleared has exectued).
It was suggested I use a package visible object that calls wait() on itself in the main game thread, and then notify() on itself from StageCleared in order to continue execution. My first problem is I can't seem to declare a package visible object that can be seen by all the classes in my package? Secondly, is this the best way to achieve what I'm intending to do or is there a better way?
Many thanks
To enable package visibile, leave the modifier blank:
static boolean mVarname = true;
mVarname is visible inside the package.
I work with a run flag to enable if the loop should do something or just "idle". I, too, dont know if this is a good way to do it :)
Use FutureTask and Executor, check it out in Java API.
It's like, you define the operation and FutureTask, and it'll wait until the task is completed.
Easier than wait()