Run program in background after the click of a button - java

I want to make my program run in the background after you click a button.
I'm creating a rickroll (in 2015 yeh) and when you click a button I want to play the song never gonna give you up from rick astley and get the frame closed and run in a process, so the song still continues. I already got the part of clicking the button and starting the song, but how can I make it continue in a process?

I do not think that "continue in a process" is what you want.
Just close your frame, allow your main message loop to end, and before returning from your main() function block waiting for the sound to end. Or, if you don't know how to block waiting for the sound to end, then calculate how many milliseconds long your sound is, and just Thread.sleep() for that many milliseconds.

Related

Set a timer for the visibility of a button

This is purely theoretical put let's say once I put a timer for a button to appear only 84600 second later (like 24h), will it the count down continue after the app is shut down ?
Can the user close the app and then come back the next day and find the button after the countdown is done ?
Or is there a better way to make it happen ? Like to allow the user to see it only after 24h ?
you can use ScheduledExecutorService to create such a timer. Then on every tick, you can open the app if you want, and show the button, or just show the button if the user open the app.
documentation is available here.

Android media player stop playing in service

I already asked about it before, but my question even after edits about my progress of fix this noone answered to it anymore. This is original question:
Android media player stop playing while in background
So in short, I'm making music player app, but mediaPlayer stop sometimes when loading next song. After many tests I find out that it stops at mediaPlayer.prepare() and it won't continue as long as I don't trigger any action on phone like turning on display, volume up/down, click headset button. I'm out of ideas what can be a reason of it.
maybe you need to call prepareasync() instead of prepare().

How to set play to a JButton one sound at a time in java?

I have been working on a project for few weeks now. This is the Main Basic UI for the Application
When User Press a button A relevant .wav file is playing but it wont't stop playing when user click another button. i want it to be stopped when user click another button.
You will have to make the current play to stop before another click ,i.e a play request comes in.
public void playClip(String clipName){
if(audioPlayer.isPlaying()){
audioPlayer.stop();
}else{
audioPlayer.setClip(clipName);
audioPlayer.play();
}// not actual code, but you get the idea

how do I use wait notify to stop pause a method while waiting on a button to be pressed in another class

So using a GUI in java, I am trying to allow the user to press a transfer button which will initiate a method. This method will open up a separate GUI which will ask the user to enter information and then save it when the user has pressed a save button. What I want to do is pause the method after it has opened the separate GUI and continue once the user has hit the save button in that second GUI. Unless there is a better way, I believe that implementing a wait notify method is the best way to go about this but I have been unsuccessful thus far.
Firstly, DON'T (use wait/notify) for any reason within the Event Dispatching Thread. This will cause the UI to stop responding to input events and repaint request, effectively "hanging" your program.
In your case, it would be a simple case of using a modal dialog.
Check out How to make dialogs and Concurrency in Swing

How to make a protection of many times pressing the button?

In my app I use a camera, and I want to take a pictures. In my app is button (Photo). If I press it one times - all work perfect, but if I press button many times until camera take picture, my app hangs. How can I fix it?
In your onClickListener call Button.setEnabled() and set it to false.
Then set it to true when you've finished taking the photo.
Use a listener that disables the button on press (setEnalbed(false)), than start a countdown thread that re-enables it after some time, 200ms maybe, or whatever fits the best.
After second thoughts this might not be a really good idea.
There is a chance that the thread will not be scheduled to run, so if you exactly know the point when you can re-enable the button in your code, don't use threads.

Categories