android-连续播放声音onTouch()

android-连续播放声音onTouch(),第1张

概述我想通过onTouch连续播放q声.我这样做是为了自动发出q声.所以我有声音循环延迟的问题.不会产生真正的自动q声效果.我的主要观点是,声音一遍又一遍地播放应该没有延迟.我的代码是.publicbooleanonTouch(Viewv,MotionEventevent){//TODOAuto-generatedmethodstu

我想通过ontouch连续播放q声.我这样做是为了自动发出q声.所以我有声音循环延迟的问题.不会产生真正的自动q声效果.我的主要观点是,声音一遍又一遍地播放应该没有延迟.
我的代码是.

public boolean ontouch(VIEw v, MotionEvent event) {    // Todo auto-generated method stub    if (event.getAction() == MotionEvent.ACTION_DOWN) {        mediaPlayer = MediaPlayer.create(RipleFire.this,                sounds[position]);        // if (mediaPlayer != null && mediaPlayer.isPlaying()) {        // mediaPlayer.stop();        // }        try {            mediaPlayer.prepare();        } catch (IllegalStateException e) {            // Todo auto-generated catch block            e.printstacktrace();        } catch (IOException e) {            // Todo auto-generated catch block            e.printstacktrace();        }        imgflag.setimageResource(pistols[position]);        imgflag.setimageResource(pistolsAnimations[position]);        mediaPlayer.start();        mediaPlayer.setLooPing(true);    } else if (event.getAction() == MotionEvent.ACTION_UP) {        mediaPlayer.stop();        mediaPlayer.reset();        mediaPlayer.release();        imgflag.setimageResource(pistols[position]);    }    return true;}

解决方法:

他们可以根据您的问题解决
方法1
只需通过启动声音即可覆盖触摸方式.

    @OverrIDe            public boolean ontouch(VIEw v, MotionEvent event) {                switch (event.getAction()) {                case MotionEvent.ACTION_DOWN:                    mPlayer.setLooPing(true);                    mPlayer.start();                    break;                case MotionEvent.ACTION_UP:                    mPlayer.pause();                    break;                }                return true;            }    //or    public boolean ontouch(VIEw v, MotionEvent me) {    switch(me.getAction()) {        case MotionEvent.ACTION_DOWN:            if(!mPlayer.isPlaying()) mPlayer.start();            break;        case MotionEvent.ACTION_UP:            if(mPlayer.isPlaying()) mPlayer.stop();            break;    }}

方法2
您还可以使用可运行的踏步或运行方法

Boolean Ispressed = false;button mbtn = (button) findVIEwByID(R.ID.mbtn);

然后添加一个线程,在方法运行中添加此代码

@OverrIDepublic voID run(){      while(!btnIspressed){          myfunction();      }}

现在,在方法onCreate中,添加一个侦听器以将布尔值更改为true,例如这样

mbtn.addOnClickListener( new OnClickListener(){   @OverrrIDe   public voID onClick(VIEw v){      btnIspressed = true;   }});

编辑:
我已经包含了另一个方法3,在这里,只要您触摸视图,我们就会运行一个线程,如果是否触摸了视图,它会检查isPress或不按下,然后重复声音,直到您从触摸中释放视图为止.

在这里,我为您发布了完整的代码

public class Soundstuff extends Activity implements OntouchListener {    SoundPool sP;    boolean isPress = false;    int gunshot = 0;    private Handler handler = new Handler();    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        VIEw v = new VIEw(this);        setContentVIEw(v);        v.setontouchListener(this);        sP = new SoundPool(2, AudioManager.STREAM_MUSIC, 0);        gunshot = sP.load(this, R.raw.gunshot, 1);    }    private Runnable runnable = new Runnable() {        @OverrIDe        public voID run() {            /* do what you need to do */            if (isPress) {                sP.play(gunshot, 1, 1, 0, 0, 1);                /* and here comes the "trick" */                handler.postDelayed(this, 300);            } else {            }        }    };    @OverrIDe    public boolean ontouch(VIEw v, MotionEvent event) {        switch (event.getAction()) {        case MotionEvent.ACTION_DOWN:            isPress = true;            handler.postDelayed(runnable, 300);            break;        case MotionEvent.ACTION_UP:            isPress = false;            handler.postDelayed(runnable, 300);            break;        }        return true;    }}

希望这能解决您的问题.

总结

以上是内存溢出为你收集整理的android-连续播放声音onTouch()全部内容,希望文章能够帮你解决android-连续播放声音onTouch()所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1079367.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-27
下一篇2022-05-27

发表评论

登录后才能评论

评论列表(0条)

    保存