-1

我在 setontouchLister 中遇到问题 我正在尝试在单击按钮时播放 10 秒剪辑,当我将手指从按钮上移开时它应该停止...时间结束,即 10 秒超时请帮助!这是我的代码

MediaPlayer mp;



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


    Button btn = (Button) findViewById(R.id.m_b);
      mp = MediaPlayer.create(MainActivity.this, R.raw.gm);




      btn.setOnTouchListener(new OnTouchListener() {


            @Override

            public boolean onTouch(View v, MotionEvent event) {

                int action = event.getAction() & MotionEvent.ACTION_MASK;

                if (action == MotionEvent.ACTION_DOWN ) {

                    mp.start();

                    return true;
                }


                return true;
            }

        });

}

@Override 

protected void onPause() {

    // TODO Auto-generated method stub

    super.onPause();

    mp.release();

    finish();

}
4

2 回答 2

0

尝试:

    if (action == MotionEvent.ACTION_DOWN ) {
        mp.start();
        return true;
    }
    else if (action == MotionEvent.ACTION_UP ) {
        mp.stop();
        return true;
    }
于 2013-11-08T21:54:52.890 回答
0

当触摸侦听器发送 ACTION_UP 事件时,您只需停止。

if (action == MotionEvent.ACTION_UP ) {
    mp.stop();
    return true;
}
于 2013-11-08T21:55:37.530 回答