issue with if/else and switch statements - java

the following code is to handle ACTION_DOWN and ACTION_UP events for a button named clash. the idea is that once if/else determines that the onTouch event was caused by clash and the switch statement then determines what to do based on the action. i don't know if the problem is that the switch statements are not returning true and that may be related to the problem. when i add a return, eclipse says that the code is unreachable which i don't understand. i was under the impression that you can't break out of a switch without break.
what's actually happening is that the first sound will loop but the code never seems to detect the action up when the button is released and so the sound plays forever. any help would be appreciated.
public boolean onTouch(View v, MotionEvent event) {
MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.clash);
if (v.getId() == R.id.clash){
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
mp.setLooping(true);
mp.start();
break;
case MotionEvent.ACTION_UP:
mp.pause();
break;
}
}
return true;
}
});

//Add the following line in the code before your setOnTouchListener()
MediaPlayer mp;
public boolean onTouch(View v, MotionEvent event) {
if (v.getId() == R.id.clash){
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mp = MediaPlayer.create(getBaseContext(), R.raw.clash);
mp.setLooping(true);
mp.start();
break;
case MotionEvent.ACTION_UP:
if(mp != null)
{
mp.pause();
mp.release();
}
break;
}
}
}
// I'm assuming this was from the onTouchListener()? -> });
Just a thought.

Related

Detect when the touch event is over in Android

How can I detect when the touch event is over in Android,in Webviews specifically.
pseudocode:
if (touch event is over) then
do something
end
Thanks!
The MotionEvent object passed to the onTouchEvent(...) method has a getAction() method, you can use it to determine if the event is over. eg:
webViews.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//pointer down.
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_OUTSIDE:
//event has finished, pointer is up or event was canceled or the pointer is outside of the view's bounds
break;
}
return false;
}
}
Also, if you want to consume the event (stop it from propagating) just make the onTouch(...) method return true instead of false.

Sound not replaying on onTouchListener

So I have a button that, when held, will play a sound. However, it does not seem to "reset" and never plays that sound again when I hold down the button again. I get a
E/MediaPlayer﹕ start called in state 0
E/MediaPlayer﹕ error (-38, 0)
error in log when I try to hold the button down again. Here is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
final MediaPlayer bark = MediaPlayer.create(this, R.raw.bark);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
playSound = (Button) findViewById(R.id.btn);
playSound.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if(!bark.isPlaying()) bark.start();
break;
case MotionEvent.ACTION_UP:
if(bark.isPlaying()) bark.stop();
break;
}
return true;
}
});
}
Instead of calling start/stop you can call start/pause with seekTo():
case MotionEvent.ACTION_DOWN:
if(!bark.isPlaying()) bark.start();
break;
case MotionEvent.ACTION_UP:
if(bark.isPlaying()){
bark.pause();
bark.seekTo(0);
}
break;
If you want to use start/stop, make sure you call prepare() before calling start() again.
The reason why your code isn't working is because according to the state diagram stop() stops playing and puts the MP in a stopped state. Before starting to play for the next time, you should call prepare() and then start() again.
EDIT : Place your music file in the assets folder and use the file descriptor. This way you can reset the MP and replay:
Using START/STOP:
#Override
protected void onCreate(Bundle savedInstanceState) {
AssetManager assetManager=Context.getAssets();
AssetFileDescriptor fileDescriptor = assetManager.openFd("bark.mp3"); //replace with right extension
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
playSound = (Button) findViewById(R.id.btn);
playSound.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if(!bark.isPlaying()){
bark.reset();
bark.setDataSource(fileDescriptor.getFileDescriptor());
bark.prepare();
bark.start();
}
break;
case MotionEvent.ACTION_UP:
if(bark.isPlaying())
bark.stop();
break;
}
return true;
}
});
}

Launch application on 2 finger swipe ANDROID

I have this code which doesn't return any errors. However it doesn't work like it's supposed to. I'm trying to launch an application when I swipe up with two fingers but nothing is happening. Can anyone see what the problem with my code?
public class HomeActivity extends Activity {
ImageButton phoneButton;
ImageButton contactsButton;
ImageButton messagesButton;
ImageButton cameraButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
setTitle("Apps");
loadApps();
loadListView();
addClickListener();
addphoneButtonListener();
addmessagesButtonListener();
addcontactsButtonListener();
addcameraButtonListener();
//Two-Finger Drag Gesture detection
RelativeLayout TextLoggerLayout = (RelativeLayout)findViewById(R.id.mainLayout);
TextLoggerLayout.setOnTouchListener(
new RelativeLayout.OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent m) {
handleTouch(m);
return true;
}
});
}
void handleTouch(MotionEvent m){
//Number of touches
int pointerCount = m.getPointerCount();
if(pointerCount == 2){
int action = m.getActionMasked();
switch (action)
{
case MotionEvent.ACTION_DOWN:
Intent i;
PackageManager manager = getPackageManager();
try {
i = manager.getLaunchIntentForPackage("com.google.android.googlequicksearchbox");
if (i == null)
throw new PackageManager.NameNotFoundException();
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
} catch (PackageManager.NameNotFoundException e) {
}
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_POINTER_DOWN:
break;
}
}
else {
//do something
}
}
Try debugging handleTouch() to see exactly why its not firing. My guess would be because you have your check on MotionEvent.ACTION_DOWN, which in real world is a bit difficult to happen exactly at the same time for two fingers.
Move the check to MotionEvent.ACTION_MOVE (which is what you want anyway cause you want swipe, not tap) and try again.
MotionEvent.ACTION_DOWN means one finger. MotionEvent.ACTION_POINTER_DOWN means secondary touches (eg. two fingers).
So moving your code to case MotionEvent.ACTION_POINTER_DOWN: should solve your issue.
Documentation.

motionEvents didn't work Android Java

i can't get message in Logcat from events : MotionEvent.ACTION_MOVE and MotionEvent.ACTION_UP. I couldn't find solution for it so i ask it here.
I have this overrided method in my class:
#Override
public boolean onTouchEvent(MotionEvent event){
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_MOVE:
Log.d("action","Action was MOVE");
break;
case MotionEvent.ACTION_DOWN:
Log.d("action","Action was DOWN");
break;
case MotionEvent.ACTION_UP:
Log.d("action","Action was UP");
break;
default:
break;
}
}
return super.onTouchEvent(event);
}
And in LogCat i can see just message from
MotionEvent.ACTION_DOWN : "action" "Action was DOWN" . No messages from other actions.
I try it in my phone and in AVD.
Thanks for any response.
Answer:
return must be set to true and not for super constructor.
Just Set return to True and not for super constructor :)

How to manage back button with multiple screens in Libgdx?

If there is some way how to manage back button in Libgdx?
for example in Andengine I have implemented this like that:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
switch (currentScene) {
case SPLASH:
break;
case MENU:
Process.killProcess(Process.myPid());
break;
case WORLDMENU:
start(MENU);
break;
...
...
}
}
}
I don't have idea how to do it here, because ApplicationListener has only create, show, render...
I tryed this:
if (Gdx.input.isButtonPressed(Keys.BACK)){
new ScreenChangeTask(MyScreen.SPLASH);
}
but it still closes my application.
FYI: I have class Controller extends Game and I use public void setScreen (Screen screen) to switch between screens.
In order to do this properly you need to tell LibGDX to catch the back key:
Gdx.input.setCatchBackKey(true);
You should do this somewhere early in the application. And set it to false when you want the user to be able to use the back key.
set
Gdx.input.setCatchBackKey(true);
then implement below code on keyUp...
#Override
public boolean keyUp(int keycode) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
switch (currentScene) {
case SPLASH:
break;
case MENU:
Process.killProcess(Process.myPid());
break;
case WORLDMENU:
game.setScreen(new MenuScreen(game)); //MenuScreen is your class Screen
break;
return false;
}
}
}
Hope this will help you

Categories

Resources