履行职能同步 [英] perform functions synchronously

查看:203
本文介绍了履行职能同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有必须要3秒同步实现的一些功能。我有一个声音用的Soundpool,振动模式,背景动画播放。他们三个都做工精细分开。但我无法掌握好火候他们让所有的启动在一起。

这三个功能都需要每15秒后要反复调用。并点击一个按钮来停止。我试图用这样的事情,但它不正常工作。

  MyThread的=新主题(新的Runnable(){            @覆盖
            公共无效的run(){
                // TODO自动生成方法存根                而(shouldRun ==真){
                    尝试{
                        mHandler.post(新的Runnable(){                            @覆盖
                            公共无效的run(){                                动画();
                                振动();
                                声音();                            }
                        });
                        mHandler.postDelayed(新的Runnable(){
                            公共无效的run(){
                            尝试{
                                视频下载(15000);
                            }赶上(InterruptedException的E){
                                // TODO自动生成catch块
                                e.printStackTrace();
                            }
                            }
                        },3000);
                    }赶上(例外五){
                        // TODO:处理异常
                    }
                }
            } //外运行完成()
        });
        myThread.start();

这是做的正确方法?或者是有一些其他的方式?

 公共无效振动(){
    INT破折号= 1000;
    INT medium_gap = 500;    长[]模式= {0,//立即开始
        破折号,medium_gap,破折号,medium_gap
    };
    //只有执行这个模式一次(-1表示不重复)
    v.vibrate(图案,-1);}
线性=(的LinearLayout)findViewById(R.id.lay); //这一行是的onCreate后的setContentView
公共无效阿尼姆(){        drawable.addFrame(新ColorDrawable(Color.RED),1000);
        drawable.addFrame(新ColorDrawable(Color.WHITE),500);
        drawable.setOneShot(假);
        linear.setBackgroundDrawable(绘制);
        drawable.start();    }SP =新的Soundpool(5,AudioManager.STREAM_NOTIFICATION,0);
蜂鸣器= sp.load(在此,R.raw.buzzer,0);
公共无效的声音(){
        sp.play(蜂鸣器,1,1,0,0,1); //启动提示音
    }


解决方案

在没有看到所有的code,我不知道是否有任何它需要在UI线程上,但可能是动画一样。我的想法是让每个在自己的线程,只是有播放,暂停,并释放方法。所以,在3秒后告诉他们暂停。然后告诉他们玩。动画将不得不从它的run方法后的东西到UI线程虽然。另外一个问题......取决于你如何接近同步需要他们,这可能是有问题的,因为在演奏音色上较旧的Andr​​oid版本可能会有显著的延迟。


编辑:您也可以更新您的问题只是给了什么不顺心在更详细一点,做自己各地同时启动,起到一个接一个或别的东西了。

下面是一些code的刺。如果添加下面的code到你的类,然后创建并启动线程懂事的地方(如视图创建)

 声音S =新的Sound();
s.start();
振动VI =新的振动(V);
vi.start();

和设置其释放真正在你的onStop()方法。最后,在处理的run方法代替:

 阿尼姆();
振动();
声音();

有:

  s.play = TRUE;
vi.play = TRUE;
动画();

//类来添加

 类Sound继承Thread
{
    公共布尔玩= FALSE;
    SP的Soundpool;
    INT蜂鸣器;
    公共布尔发布= FALSE;    市民声音()
    {
        SP =新的Soundpool(5,AudioManager.STREAM_NOTIFICATION,0);
        蜂鸣器= sp.load(在此,R.raw.buzzer,0);
    }    公共无效的run()
    {
        而(!发布)
        {
            如果(播放)
            {
                sp.play(蜂鸣器,1,1,0,0,1); //启动提示音
                玩= FALSE;
            }
            其他
            {
                尝试
                {
                    视频下载(5);
                }
                赶上(例外前){}
            }
        }        //释放声音游泳池
    }
}一流的振动继承Thread
{
    公共布尔玩= FALSE;
    公共布尔发布= FALSE;
    INT破折号= 1000;
    INT medium_gap = 500;
    振动器伏;    长[]模式= {0,//立即开始
        破折号,medium_gap,破折号,medium_gap};    公共振动(振动器V)
    {
        this.v = V;
    }    公共无效的run()
    {
        而(!发布)
        {
            如果(播放)
            {
                //只有执行这个模式一次(-1表示不重复)
                v.vibrate(图案,-1);
                玩= FALSE;
            }
            其他
            {
                尝试
                {
                    视频下载(5);
                }
                赶上(例外前){}
            }
        }
    }
}

i have a few functions that have to be implemented synchronously for 3 seconds. i have a sound being played using soundpool, a vibration pattern, and a background animation. all three of them work fine separately. but i am not able to time them properly so that all start together.

these 3 functions need to be called repeatedly after every 15 seconds. and will be stopped on a button click. i tried using something like this, but it doesnt work properly.

 myThread = new Thread(new Runnable(){

            @Override
            public void run() {
                // TODO Auto-generated method stub

                while (shouldRun==true) {
                    try {                      
                        mHandler.post(new Runnable() {

                            @Override
                            public void run() {

                                anim();
                                vibration();
                                sound();



                            }
                        });
                        mHandler.postDelayed(new Runnable() {
                            public void run(){
                            try {
                                Thread.sleep(15000);
                            } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            }
                        }, 3000);
                    } catch (Exception e) {
                        // TODO: handle exception
                    }
                }               
            }// end of outer run()          
        });
        myThread.start();

is this the right way to do it? or is there some other way?

public void vibration(){            
    int dash = 1000;    
    int medium_gap = 500;

    long[] pattern = { 0,  // Start immediately
        dash, medium_gap,dash , medium_gap
    };
    // Only perform this pattern one time (-1 means "do not repeat")
    v.vibrate(pattern, -1);

}


linear = (LinearLayout) findViewById(R.id.lay);// this line is after setContentView in onCreate
public void anim(){

        drawable.addFrame(new ColorDrawable(Color.RED), 1000);  
        drawable.addFrame(new ColorDrawable(Color.WHITE), 500);         
        drawable.setOneShot(false);     
        linear.setBackgroundDrawable(drawable);         
        drawable.start(); 

    }

sp = new SoundPool(5, AudioManager.STREAM_NOTIFICATION, 0);
buzzer = sp.load(this, R.raw.buzzer, 0);
public void sound(){
        sp.play(buzzer, 1,1, 0, 0, 1); //start alert tone
    }

解决方案

Without seeing all the code I am not sure if any of it needs to be on the UI thread, but probably the animation does. My thought was to have each in its own thread, and just have a play, pause, and release methods. So after 3 seconds tell them all to pause. And then tell them all to play. The animation will have to post something to the UI thread from its run method though. One other problem ... depending on how closely synchronized you need them this may be problematic because there may be a significant delay in playing sounds on older android versions.


EDIT: Can you also update your question to just give a little more detail of what goes wrong as in, do they all start around the same time, play one after the other or something else.

Here is a stab at some of the code. If you add the below code to your class, then create and start the threads somewhere sensible (like the view creation)

Sound s = new Sound();
s.start();
Vibrate vi = new Vibrate(v);
vi.start();

and set their release to true in your onStop() method. Finally, in run method of your handler instead of:

anim();
vibration();
sound();

have:

s.play = true;
vi.play = true;
anim();

// classes to add

class Sound extends Thread
{
    public boolean play = false;
    SoundPool sp;
    int buzzer;
    public boolean released = false;

    public Sound()
    {
        sp = new SoundPool(5, AudioManager.STREAM_NOTIFICATION, 0);
        buzzer = sp.load(this, R.raw.buzzer, 0);
    }

    public void run()
    {
        while (!released)
        {
            if (play)
            {
                sp.play(buzzer, 1,1, 0, 0, 1); //start alert tone
                play = false;
            }
            else
            {
                try
                {
                    Thread.sleep(5);
                }
                catch (Exception ex) {}
            }
        }

        // release the sound pool
    }
}

class Vibrate extends Thread
{
    public boolean play = false;
    public boolean released = false;
    int dash = 1000;    
    int medium_gap = 500;
    Vibrator v;

    long[] pattern = { 0,  // Start immediately
        dash, medium_gap, dash , medium_gap };

    public Vibrate(Vibrator v)
    {
        this.v = v;
    }

    public void run()
    {
        while (!released)
        {
            if (play)
            {
                // Only perform this pattern one time (-1 means "do not repeat")
                v.vibrate(pattern, -1);
                play = false;
            }
            else
            {
                try
                {
                    Thread.sleep(5);
                }
                catch (Exception ex) {}
            }
        }
    }
}

这篇关于履行职能同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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