Combining Next and Forward function as one button in android eclipse

643 views Asked by At

I am creating an android music player.I am going to make both the Next and Forward button work in one button depending on the duration it's held down. In a short press it works as the Next button and in long press it works as the forward button. I have tried this using onLongClickListener but what happen is, it stops forwarding even before the user holds up the button. How do I implement it in the way where the action doesn't stop till the user hold up the button? Here is my code. Thanks in advanced.

        btnNext.setOnLongClickListener(new OnLongClickListener() {

        @Override
        public boolean onLongClick(View arg0) {
            // get current song position
            int currentPosition = mp.getCurrentPosition();

            // check if seekForward time is lesser than song duration
            if (currentPosition + seekForwardTime <= mp.getDuration()) {
                // forward song
                mp.seekTo(currentPosition + seekForwardTime + LONG_CLICK_LISTERNER_INTERVAL);
            } else {
                // forward to end position
                mp.seekTo(mp.getDuration());
            }
            return true;

        }
    });
1

There are 1 answers

0
Charan Pai On

See this post of mine Events for Long click down and long click up in Android

If you want to detect touch events, which is called MotionEvent in Android, you have to override onTouchEvent(MotionEvent e) method and use GestureDetector class for identifying long press.

private GestureDetector mGestureDetector;

public FfwRewButton(...) {
    //....
    mGestureDetector = new GestureDetector(context, 
        new GestureDetector.SimpleOnGestureListener() {
            public boolean onDown(MotionEvent e) {
                mLongClicked = false;
                return true;
            }
            public void onLongPress(MotionEvent e) {
                mLongClicked = true;
                // long press down detected
            }
    });
}

public boolean onTouchEvent(MotionEvent e) {
    mGestureDetector.onTouchEvent(e);
    if (mLongClicked && e.getAction() == ACTION_UP) {
           // long press up detected
        }
    }
}

on long press down you can start a thread doing same in a loop and stop on long press up , hope this helps you