【问题标题】:Can anyone deal with it?任何人都可以处理它吗?
【发布时间】:2016-10-08 10:04:50
【问题描述】:

当我将鼠标悬停在按钮上并停在 HOVER_EXIT 处时,我希望我的按钮(应该是按钮还是其他?)播放音乐。我应该实现什么 case MotionEvent.ACTION_HOVER_MOVE: 让按钮继续播放音乐,从ENTER 到EXIT 和MOVE 之间没有停顿@ 什么都不做? 还有一个错误 - 当我尝试使用此悬停按钮打开屏幕时,应用程序崩溃并关闭。 这是我的java代码:

import android.content.Intent;
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;

public class DisplayActivity extends AppCompatActivity {

Button b1;

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

        @Override
        public boolean onHover(View v, MotionEvent event) {
            MediaPlayer player=MediaPlayer.create(DisplayActivity.this,R.raw.sound);
            switch (event.getAction()) {
                case MotionEvent.ACTION_HOVER_ENTER:
                   player.start();
                    break;
                case MotionEvent.ACTION_HOVER_EXIT:
                    player.stop();
                    break;
            }
            return true;

        }
    });}`

【问题讨论】:

  • 可以分享logcat吗?
  • 这里是错误行:java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.appname/com.example.android.appname.DisplayActivity}: java.lang.ClassCastException: android.support.v7.widget.AppCompatButton cannot be cast to android.widget.ImageView
  • 看起来,在您的代码中某处,您正试图将 Button 的引用分配给 ImageView。问题中提到的代码是否完整?
  • @user3161880 现在一切正常,但是当我将鼠标悬停在我的按钮上时,音乐不播放:(你有任何关于如何解决这个问题的想法吗?

标签: android media-player onhover


【解决方案1】:
public class DisplayActivity  extends AppCompatActivity {

    private MediaPlayer player;

    @Override
    protected void onResume() {
        super.onResume();

        // create media player only when required
        player = MediaPlayer.create(this, R.raw.sound);

        // this will keep the audio playing, even if you hover for long time
        player.setLooping(true);
    }


    @Override
    protected void onPause() {
        super.onPause();

        // release is as soon as possible
        player.release();
        player = null;
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_screen);

        findViewById(R.id.button1).setOnHoverListener(new View.OnHoverListener() {

            @Override
            public boolean onHover(
                    View v,
                    MotionEvent event) {
                switch (event.getAction()) {
                case MotionEvent.ACTION_HOVER_ENTER:
                    player.start();
                    break;
                case MotionEvent.ACTION_HOVER_EXIT:
                    // if you choose to stop the player, you need to prepare it again by calling player.prepare(); before restarting it.
                    // I chose to pause it and seek it back to beginning
                    player.pause();
                    player.seekTo(0);
                    break;
                }
                return true;
            }
        });
    }
}

希望这会有所帮助:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-14
    • 2013-10-09
    • 2012-10-28
    • 2011-03-31
    • 1970-01-01
    • 1970-01-01
    • 2019-08-07
    • 2021-01-14
    相关资源
    最近更新 更多