【问题标题】:Add an onTouch event for Action_Down and _up to a button during runtime to play a sound while button is pressed在运行时将 Action_Down 和 _up 的 onTouch 事件添加到按钮以在按下按钮时播放声音
【发布时间】:2013-04-09 03:54:35
【问题描述】:

我想在运行时创建一个按钮。按钮应在按下时开始播放声音,并在用户停止按下按钮时停止播放。

浏览网页和 Stack Overflow 我想出了这段代码:

    // Create a new button and place it into a table row
    LinearLayout lnr = (LinearLayout) findViewById(R.id.tableRow3);
    Button b1 = new Button(this);
    lnr.addView(b1);

    // Associate the event
    b1.setOnTouchListener(new OnTouchListener() {
        MediaPlayer mp = new MediaPlayer();
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch(event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                // Finger started pressing --> play sound in loop mode
                try {
                    FileInputStream fileInputStream = new FileInputStream( PATH );
                    mp.setDataSource(fileInputStream.getFD());
                    mp.prepare();
                    mp.setLooping(true);
                    mp.start();
                } catch (Exception e) {}
            case MotionEvent.ACTION_UP:
                // Finger released --> stop playback
                try {
                    mp.stop();
                } catch (Exception e) {} 
          }
          return true;
        }
      });   

问题是我根本听不到声音。在我看来,case MotionEvent.ACTION_UP: 是直接触发的。所以直接停止播放。

为了验证这个假设,我删除了mp.stop(); 并听到了无限循环的声音。很明显,一定是 ACTION_UP 事件搞砸了一切。但是不松开手指/鼠标怎么触发ACTION_UP事件呢?

【问题讨论】:

    标签: android button media-player ontouchlistener


    【解决方案1】:

    您应该在“case MotionEvent.ACTION_DOWN”的底部插入“break”。

    【讨论】:

    • 当然......我非常担心找到 MediaPlayer 对象或 MotionEvent 的问题,以至于我错过了明显的问题。谢谢!我已经更正了代码并将其附在下面。
    • 我刚刚意识到这只能工作一次。我单击按钮并听到声音循环。我松开按钮,播放立即停止。还必须添加 mp.reset(); mp.stop() 之后;
    【解决方案2】:

    正确的代码是:

        // Create a new button and place it into a table row
        LinearLayout lnr = (LinearLayout) findViewById(R.id.tableRow3);
        Button b1 = new Button(this);
        lnr.addView(b1);
    
        // Associate the event
        b1.setOnTouchListener(new OnTouchListener() {
            MediaPlayer mp = new MediaPlayer();
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch(event.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_DOWN:
                    // Finger started pressing --> play sound in loop mode
                    try {
                        FileInputStream fileInputStream = new FileInputStream( PATH );
                        mp.setDataSource(fileInputStream.getFD());
                        mp.prepare();
                        mp.setLooping(true);
                        mp.start();
                    } catch (Exception e) {}
                break;
                case MotionEvent.ACTION_UP:
                    // Finger released --> stop playback
                    try {
                        mp.stop();
                        mp.reset();
                    } catch (Exception e) {}
                break;
              }
              return true;
            }
          });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-02
      • 2015-05-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多