How to play and stop sound by one button using Soundpool - android

I’m new one here and I’m kind of a new one in java. All my knowledge about code based on what I can find in web for my project. So in one hand I know some difficult stuff on the other hand I don’t know a simple things. Right now I’m stuck in this situation.
I have button with looped sound what I’m trying to do is when i touch button it should change image on "button_prees" when i release button it should play sound and change image on "button_on" So for that moment code below work fine but now I’m trying to do reverse sequence. You touch the same button and it change image on "button_prees2" than when you release button it should stop playing sound and change image on "button_off"
How implement this in code below ?
final Button b1 = (Button) findViewById(R.id.button1);
b1.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()){
case MotionEvent.ACTION_DOWN:
b1.setBackgroundResource(R.drawable.button_prees);
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
myS.play(s1I, 1, 1, 1, -1, 1);
b1.setBackgroundResource(R.drawable.button_on);
break;
}
return true;
}
});

You could use a boolean as a flag for the Button state. Very briefly and basically:
boolean soundsOn = false;
public void setSoundsOn(boolean soundsOn){
this.soundsOn = soundsOn;
}
public boolean isSoundsOn(){
return soundsOn;
}
...
// Then for your Button listener:
onTouch(...)
if (!isSoundsOn){
// Your above above code to change resources, start playing
// EXCEPT
case MotionEvent.ACTION_UP:
...
setSoundsOn(true);
...
}else{
// Code to stop etc.
// INCLUDING
case MotionEvent.ACTION_UP:
...
setSoundsOn(false);
...
}
...
Ideally you should make sure sound has successfully started/stopped before calling setSoundsOn(...) otherwise your labels might end up saying what is not. Hope this helps...

Related

Android - change image of ImageButton when pressed and release

I would like to set image1 for the button, but when it is pressed - change it to image2. After releasing, the button should be again an image1. I tried to do it this way in onClick() method:
button.setImageResource(R.drawable.image1);
if(button.isPressed())
button.setImageResource(R.drawable.image2);
but after first pressing the image of button changed to the image2 and stayed like that.
Could You help me with that problem?
I think this is what you want:
MyCustomTouchListener myCustomTouchListener = new MyCustomTouchListener();
button.setOnTouchListener(myCustomTouchListener);
Now the MyCustomTouchListener :
class MyCustomTouchListener implement OnTouchListener {
public boolean onTouch(View v, MotionEvent event)
{
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
// touch down code
button.setImageResource(R.drawable.image1);
break;
case MotionEvent.ACTION_MOVE:
// touch move code
break;
case MotionEvent.ACTION_UP:
// touch up code
button.setImageResource(R.drawable.image1);
break;
}
return true;
}
}
You can do this easily with a state list drawable, and it requires no additional java code on your part (unless you are creating a StateListDrawable at runtime, but even that is a more suitable approach than implementing custom touch interaction).
Use the following:
int dispImg = 0;
button.setImageResource(R.drawable.image1);
if (button.isPress()) {
if (dispImg == 0) {
button.setImageResource(R.drawable.image2);
dispImg = 1;
}
else if (dispImg == 1) {
button.setImageResource(R.drawable.image1);
dispImg = 0;
}
}
Explanation: dispImg keeps track of the image you're showing. When it is 0, it means that the 1st Image is showing and so, we should switch to the 2nd.
Hope I Helped :D

Using of MotionEvent (ACTION_MOVE)

So I have special question to you:
How can I catch buttons when I use move action? How can I handle moves on my screen for buttons(or other elements)?
I used MotionEvent (ACTION_MOVE) but by using that fragment of code i don't get the desired result
in OnCreate
btn1.setOnTouchListener(this); //for all of buttons on Activity
in onTouch
switch(event.getAction()) {
case MotionEvent.ACTION_MOVE:
//actions
break;
}
actions will be occur for first button from which the movement starts. In other case nothing will occur
At this time I think that I can use ACTION_MOVE for my Activity not for each button or other elements and save coordinates of buttons(top left and bottom right) to arraylist.
And when movement starts I could compare this coordinates and real coordinates of movement.
So by that way I could know on which buttons movement was.
Probably I reinvent the wheel. That why I ask for your help)
Would you explain more clear? From what I see you need probably do two things:
First add listener to all buttons and let your activity implement onTouchListener.
Second you need to save initial coordinate for each button in MotionEvent.ACTION_DOWN(if I'm right about syntax).
This way you will have access to all buttons of yours and you can save their coordinates correctly
You will get the view inside the touch listener, using which you can identify.
Button btn1 = (Button) findViewById(R.id.btn1);
Button btn2 = (Button) findViewById(R.id.btn2);
Button btn3 = (Button) findViewById(R.id.btn3);
btn1.setOnTouchListener(touchListener);
btn2.setOnTouchListener(touchListener);
btn3.setOnTouchListener(touchListener);
private OnTouchListener touchListener = new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE) {
switch (v.getId()) {
case R.id.btn1:
//Button 1
break;
case R.id.btn2:
//Button 2
break;
case R.id.btn3:
//Button 3
break;
}
}
return false;
}
};

detect a continuous press on View

I need to play a certain sound as long as the user holds an ImageView pressed and pause it when the user stop holding it. Before doing anything with the MediaPlayer, I tried to test it with a Vibrator(please don't laugh at this point) object. I've implemented and set an OnTouchListener:
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
vib.vibrate(50);
break;
case MotionEvent.ACTION_MOVE:
/*--- no action required ---*/
break;
case MotionEvent.ACTION_UP:
vib.cancel();
break;
}
return false;
}
However, the vibrate action is only performed once, no matter how long I hold the ImageView pressed. I guess a MediaPlayer will react the same way. What am I doing wrong?
P.S. the press is detected correctly, because the Vibrator logs "cancel()" only after I release my finger.
It's stop because in function
vib.vibrate(50);
you set to 50 ms time in vibration check
void vibrate(long milliseconds)
Vibrate constantly for the specified period of time.
I think rest of your code is correct.

Touching the screen while onTouch() is already being called

When I touch the screen and move my finger I do something (pullanimation1 and 2) and when I release the screen I do something else (fireanimation1 and 2). Sometimes, the user might touch the screen while pullAnimation or fireAnimation is running, I get errors as the animation then run several times. I would like to make sure the animations won't run more then once when the user touch again the screen.
NB: pullAnimation1 and 2, fireAnimation 1 and 2 are AnimationDrawable
Here is what I've done :
image2.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
boolean bool=false;
boolean bool2=true;
int action = arg1.getAction() & MotionEvent.ACTION_MASK;
switch (action) {
case MotionEvent.ACTION_DOWN:
if (bool2) {
startAnimation(pullAnimation1,pullAnimation2);
bool=true;
}
break;
case MotionEvent.ACTION_MOVE:
if (bool2==true){
Log.w("GAMEACTIVITY","move");
bool=true;
bool2=false;
}
break;
case MotionEvent.ACTION_UP:
startAnimation(fireAnimation1,fireAnimation2);
bool=false;
doPhotoTask();
bool2=false;
break;
}
return bool;
}
});
I think you should be able to use the hasStarted() and hasEnded() methods to determine if your animation is currently going. See the docs for more
Some if statement like this might work:
if((fireAnimation1.hasStarted() == false) || (fireAnimation1.hasEnded == true()){
startAnimation(fireAnimation1, fireAnimation2);
}
I imagine you may also need to use reset() after it is done playing in order or the methods to return proper values next time touch happens.
EDIT:
AnimationDrawable has an isRunning() method, which makes it even easier than View animations.
if(fireAnimation1.isRunning() == false){
startAnimation(fireAnimation1, fireAnimation2);
}

How to slide to perform clicks android

I'm trying to make an application that the user can slide or drag the finger across a set of buttons to perform a click. I've tried onTouch with MotionEvent.ACTION_MOVE but I can't seem to get it to work. The buttons just don't press at all. Here's the code :
public boolean onTouch(View v, MotionEvent arg1) {
if (arg1.getAction() == MotionEvent.ACTION_MOVE) {
switch (v.getId()) {
case R.id.key1:
key1.performClick();
break;
case R.id.key2:
key2.performClick();
break;
case R.id.key3:
key3.performClick();
break;
case R.id.key4:
key4.performClick();
break;
case R.id.key5:
key5.performClick();
break;
case R.id.key6:
key6.performClick();
break;
case R.id.key7:
key7.performClick();
break;
case R.id.key8:
key8.performClick();
break;
}
}
return true;
}
I think you are better off capturing the key events on all those buttons and then performing some sort of sum on each one. For example
Key1 = 1
key2 = 1+2
key3 = 1+2+3
...
When you reach keyx, you can check if the sum matches your expectation. If it does, the user probably swiped his / her finger across the buttons. Use some sort of timing by which keyx must be pressed and the numbers must add up to N. That will avoid the user pressing button3 and then coming back to button1 etc and still registering a click.
If you want to register a click on each button, set their onclick listeners and ensure that the buttons themselves are clickable (they should already be).

Categories

Resources