【问题标题】:Android two sounds when pressing buttonAndroid按下按钮时有两种声音
【发布时间】:2012-06-24 01:22:55
【问题描述】:

我在按下按钮时尝试使用两种声音:一种是按下按钮时发出的声音,另一种是释放按钮时发出的声音。我怎样才能做到这一点?

我目前在点击按钮时播放声音:

    button.setOnClickListener(new OnClickListener() {
        public void onClick(View view) {

            mp = MediaPlayer.create(Test.this, R.raw.mysound);   
            mp.start();
            mp.setOnCompletionListener(new OnCompletionListener() {

                @Override
                public void onCompletion(MediaPlayer mp) {
                    // TODO Auto-generated method stub
                    mp.release();
                }

            });
        }

    });

【问题讨论】:

  • 您需要自己处理onTouch,以便检测按钮何时被释放。

标签: android button audio


【解决方案1】:

尝试使用OnTouchListener

public boolean onTouch(View v, MotionEvent ev) {
    int action = ev.getAction() & MotionEvent.ACTION_MASK;
    if (action == MotionEvent.ACTION_DOWN) {
        // Play "click" sound
    } else if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
        // Play "unclick" sound
    }
}

您可以使用setOnTouchListener() 将其附加到OnTouchListener,就像您已经使用OnClickListener 演示的那样。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-27
    • 2013-02-17
    相关资源
    最近更新 更多