I'm using Gluon and have an audio player. When I call the audio and play it all works normally until I press the home button. What I want it to do is to stop the music not to continue playing as it does now. Currently I've tried using an event listener to capture the event and stop the music but it's not recognising the event, I'm thinking either I have assigned the wrong keycode or it simply doesn't work like that. I have a setOnHiding method in the view already and that only works on the back button. I've also tried setOnHidden and setOnCloseRequest as well with no luck. The event listener is below.
if (event.getCode().equals(KeyCode.HOME) && KeyEvent.KEY_PRESSED == event.getEventType()) {
if (service1 != null) {
service1.backPressed();
}
Add a Listener to LifecycleEvent.Pause:
Services.get(LifecycleService.class).ifPresent(s -> s.addListener(LifecycleEvent.PAUSE, () -> stopPlayback());
The PAUSE event is fired when an application loses focus (e.g. on Android / iOS when the focus is switched out of view (but still running in the background)).
Related
I want to make an Android TV app which behaves like traditional TV, when the up and down button on a remote control is pressed, TV channel is switched. I have mapped the onKeyDown listener. However, everytime I press a button, "playback control glue" shows up. I need to press the button again to trigger the listen so as to switch channel.
Playback Control Glue:
https://developer.android.com/training/tv/playback/transport-controls
Is there any way to disable the
control glue" from showing up? Since my app plays live streams, I don't want user to pause the video, and seeking is not possible.
And also, how could the listen get the keydown event immediately without going through the "control glue"?
Thanks.
P.S. I am not familiar with Android development. I am modifying the TV sample code in Kotlin.
If you want to handle the key events in the activity, and not go through the control glue, you can override dispatchKeyEvent in your activity.
Like this:
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_UP
|| event.geyKeyCode() == KeyEvent.KEYCODE_DPAD_DOWN) {
//switch channel
return true;
}
return super.dispatchKeyEvent(event);
}
What is the equivalent of GetKey for buttons. In my android game when user press and holds the button , i want player to move right but it only moves for one time when i press the button.But when i get input from a keyboard with GetKey function , it moves until i stop holding that button. I want to use my button like GetKey.I tried to add a event trigger pointer down component into my button but it is not working.What should i do?
public void moveRight(){
rigidbody2D.velocity = new Vector2(moveSpeed,rigidbody2D.velocity.y);
}
void Update () {
if(Input.GetKeyDown (KeyCode.Space)){
rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, jumpHigh);
}
if (Input.GetKey (KeyCode.A)) {
rigidbody2D.velocity = new Vector2(-moveSpeed,rigidbody2D.velocity.y);
}
if (Input.GetKey (KeyCode.D)) {
rigidbody2D.velocity = new Vector2(moveSpeed,rigidbody2D.velocity.y);
}
}
they are absolutely same but i don't understand why its not working continously.
Update method is the key, i know this but i don't know how to get information about is a button pressed in Update method.Stuck help.
Well, we have two different parts of Unity3D API here.
Let's split it up into pieces:
Input.GetKey
As stated in API docs, Input.GetKey just checks if certain key is pressed:
Returns true while the user holds down the key identified by name. Think auto fire.
If you check it every frame, (Update() is executed every frame) you re-check if button is pressed and if so, proceed with velocity change.
EventTrigger
EventTriggers are a little different. They actually bind a delegate (UnityAction to be precise) you pass to them and trigger it as callbacks on given EventTriggerType fired. So if you bind to OnPointerDown() with your moveRight(), it will fire when pointer goes down over GameObject. If you keep the button pressed, it won't fire again because actual event of pointer going down has already happened and it already fired appropriate trigger you passed to it. Pointer is actually not going down right now. It's pressed.
It all goes down to the fact that one part checks every frame if button is pressed while the other one fires when button press happens.
I am creating an application. At one view I have three states. If I changes state and than back button pressed it unloads the application.
I am developing it in Adobe AIR.
any one have any idea how to fix it.
Thanks
States are not views, they do not get stacked in the navigator. Back button will never go backward for your states. The back button will drop the view you push until it reach the view 0, then it'll exit the application. You can monitor what's going on with the back button by listening like this:
systemManager.stage.addEventListener(KeyboardEvent.KEY_UP, handleCustomDeviceKeyUpHandler,false,10000);
private function handleCustomDeviceKeyUpHandler(event:KeyboardEvent):void {
trace("handleCustomDeviceKeyUpHandler()")
var key:uint = event.keyCode;
if (key == Keyboard.BACK) {
event.preventDefault(); //Comment if you don't want to prevent.
}
}
I have an activity that essentially launches a CountDownTimer. I need this to continue running while using other Android apps, such as Gmail, and media players etc.
When I hit the back button, my Activity seems to quit. What do I need to do to keep it running when the user clicks the back/home keys etc.
You have to use a Service, you can read the documentation here.
Activities are not supposed to be doing anything when they are not visible. They are basically UI components.
You should be looking into services. These can run as a background process nd contain no UI.
It's been referred to as a serious "hack" by a few developers, but I've successfully checked for the back button being pressed so as to successfully clean my project up. I've recently moved most of this code to onPause() event because it's called right before the app goes down after back button is pressed. However, if it's really what you desire, it's your program and here's the code I used:
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
killAndReturnParams(0);
}
Again, this method has been talked down before, but I'm pretty sure it's because you're not supposed to override the back button for any reason. The user is supposed to be able to use the back button to return at any point, in any app.
#Override
public void onBackPressed() {
mActivity.moveTaskToBack(true);
}
i've been searching the web but i find no answer to my question. I have a button, when you press it, it will play a sound. The problem is that when you touch a button on the screen it goes to the onClickListener() only after the button have been released. I need it to run the listener when the button is pressed not when it's released, because this cause a delay when playing the sound. I tried onTouchListener() and it didn't work either, because the sound get's played every time i move the finger over the button. I tried onKeyDown() but it won't work for screen buttons. Any ideas? Some help will be appreciated.
Thanks
You can use OnTouchListener and test the event action:
public boolean onTouch (View v, MotionEvent event) {
if (event.getAction () == MotionEvent.ACTION_DOWN) { // ...
}
}
Maybe you can still use your implementation for onTouchListener(). Just set a boolean flag for when the user starts touching the button, then while it is set true (meaning the user hasn't released the button yet) do not play the sound. When the user releases the button (ACTION_UP), set the flag back to false. This would mean you are ready to play the sound again.