如何通过onTouch播放声音和停止时fingures起来 [英] How to play sound by onTouch and stop when fingures up

查看:194
本文介绍了如何通过onTouch播放声音和停止时fingures起来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我触摸图像,媒体播放器开始,但是,从图像中消除fingure,媒体playes一点儿也不stop.Please帮我家伙。

iv.setOnTouchListener(本);
MP1 = MediaPlayer.create(getApplicationContext(),R.raw.air);

  @覆盖
    公共布尔onTouch(视图V,MotionEvent事件){
        // TODO自动生成方法存根        开关(event.getAction())
        {
        案例MotionEvent.ACTION_DOWN:            MP1 = MediaPlayer.create(getApplicationContext(),R.raw.airhorn_1);
            mp1.start();
            mp1.setLooping(真);
            mp1.setOnCompletionListener(新OnCompletionListener(){                公共无效onCompletion(MediaPlayer的MP1){
                    // TODO自动生成方法存根
                    mp1.release();
                }
            });            Log.i(动作,下移);
            打破;        案例MotionEvent.ACTION_MOVE:
            Log.i(动作,移动);
            Log.i(长,长);            打破;        案例MotionEvent.ACTION_CANCEL:            mp1.pause();            Log.i(动作,取消);
            打破;        案例MotionEvent.ACTION_UP:            mp1.pause();
            //mp1.stop();
            Log.i(动作,向上);
            打破;        默认:
            打破;
        }
        返回true;
    }


解决方案

 公共无效onTouch(视图v){
            如果(mp.isPlaying()){
                mp.stop();
            }其他{
                mp.start();
            }

当你触摸的ImageView如果媒体播放器播放,如果是它会阻止它,如果没有它会启动它这将检查。有了这个,你不应该需要使用开关的情况下。

请务必在此之前设置您的媒体播放器。

When I touch on Image, the media player starts but on removing fingure from that image, media playes does'nt stop.Please help me guys.

iv.setOnTouchListener(this); mp1 = MediaPlayer.create(getApplicationContext(), R.raw.air);

@Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub

        switch (event.getAction()) 
        {
        case MotionEvent.ACTION_DOWN:

            mp1 = MediaPlayer.create(getApplicationContext(), R.raw.airhorn_1);
            mp1.start();
            mp1.setLooping(true);
            mp1.setOnCompletionListener(new OnCompletionListener() {

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

            Log.i("Action", "Down");
            break;

        case MotionEvent.ACTION_MOVE:
            Log.i("Action", "Move");


            Log.i("On Long", "On Long");

            break;

        case MotionEvent.ACTION_CANCEL:

            mp1.pause();

            Log.i("Action", "Cancel");
            break;

        case MotionEvent.ACTION_UP:

            mp1.pause();
            //mp1.stop();
            Log.i("Action", "up");
            break;

        default:
            break;
        }
        return true;
    }

解决方案

public void onTouch(View v) {
            if (mp.isPlaying()) {
                mp.stop();
            } else {
                mp.start();
            }

This will check when you Touch the ImageView if the Media Player is playing, if it is it will stop it, if not it will start it. With this you shouldn't need to use the switch case.

Be sure to set up your media player before this.

这篇关于如何通过onTouch播放声音和停止时fingures起来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆