-1

What do I need to do to make my button (or sth else, please tell me what should i use):

  • Start playing music on ACTION_HOVER_ENTER

  • Keep playing (without reset, just do nothing) on ACTION_HOVER_MOVE

  • Stop playing and reset music on ACTION_HOVER_EXIT

  • Work when I start my move from anywhere on the screen, then without release hover enter my button

  • Have no problem with starting the same activity but triggered by another button located in another part of the screen - without finger's release

I should use onHoverListener(), am I right?

Please write code sample if you can :)

Here is what do I have so far:

package com.example.android.appname;

import android.content.Intent;
import android.graphics.Rect;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class DisplayActivity extends AppCompatActivity {

ImageView b1;
MediaPlayer mPlayer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_screen);
    b1 = (ImageView) findViewById(R.id.button2);
    b1.setOnHoverListener(new View.OnHoverListener() {


        @Override
        public boolean onHover(View v, MotionEvent event) {
            mPlayer = MediaPlayer.create(DisplayActivity.this, R.raw.sound);
            switch (event.getAction()) {


                case MotionEvent.ACTION_HOVER_ENTER:
                    if (mPlayer == null)
                        mPlayer.start();
                    break;

                case MotionEvent.ACTION_HOVER_MOVE:
                    break;

                case MotionEvent.ACTION_HOVER_EXIT:
                    mPlayer.release();
                    mPlayer = null;
                    break;
            }
            return false;

        }
    });
}
Community
  • 1
  • 1
Rediner
  • 83
  • 1
  • 7
  • This isn't a place where you ask people to write code for you. We're here to help answer questions and solve problems for people who've put in the effort to try themselves. – mwieczorek Oct 08 '16 at 18:46
  • I am just looking for some help, because I was writing my code for a long time, it is still not working, so I decided to look for an advice on this forum. – Rediner Oct 08 '16 at 18:51
  • Just added my work, @mwieczorek – Rediner Oct 08 '16 at 18:59

1 Answers1

0

Try implementing this way

It would require you to tell the System that you've consumed the Event and interested in consumption of successive Events. This can be done by returning true (expressing interest), false (expressing no interested).

@Override
public boolean onHover(View v, MotionEvent event) {

    mPlayer = MediaPlayer.create(DisplayActivity.this, R.raw.sound);

    // Flag to indicate interest
    boolean consumable = false;

    switch (event.getAction()) {

        case MotionEvent.ACTION_HOVER_ENTER:
            if (mPlayer == null) {
                mPlayer.start();
                consumable = true;
            }
            break;

        case MotionEvent.ACTION_HOVER_MOVE:
            consumable = true;
            break;

        case MotionEvent.ACTION_HOVER_EXIT:
            mPlayer.release();
            mPlayer = null;
            consumable = true;
            break;
    }

    return consumable;
}

Note: There might be Hardware Limitations to detect any Hover.

Community
  • 1
  • 1