简体   繁体   English

handler.postDelayed不起作用(android eclipse)

[英]handler.postDelayed is not working(android eclipse)

I am trying to make a delay after playing the set of sounds in mediaplayer so that it won't overlap. 我正在尝试在Mediaplayer中播放一组声音后进行延迟,以使其不会重叠。 i have tried thread.sleep and it worked but the problem is I have a button for sound to stop but because of the thread.sleep, it is not working so i searched and tried for an alternative way in which i can make a delay without locking the UI and i came up with handler.postDelayed but it is not working. 我尝试过thread.sleep并成功了,但问题是我有一个声音停止按钮,但是由于thread.sleep不能正常工作,所以我搜索并尝试了一种替代方法,可以在没有延迟的情况下进行延迟锁定用户界面,我想出了handler.postDelayed,但它不起作用。 is there anyone who can help me solve this problem? 有谁可以帮助我解决这个问题? thanks in advance. 提前致谢。

here is my code: 这是我的代码:

protected void managerOfSound() {
    int size = tempq.size();
    for (int i = 0; i < tempq.size(); i++) {
        String u =tempq.get(i);

    //WHOLE
        if (u.equals("a4")){

            mp = MediaPlayer.create(this, R.raw.a4);
            mp.start();
            final Runnable r = new Runnable() {
                public void run() {

                    handler.postDelayed(this, 2000);
                }
            };
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
  @Override
  public void run() {
    // stuff you wanna delay
  }
}, 2000);  

The postDelayed(Runnable r, long delayMillis ) causes the Runnable to be added to the message queue, to be run after the specified amount of time elapses. postDelayed(Runnable r,long delayMillis)使Runnable添加到消息队列,并在经过指定的时间后运行。 The runnable will be run on the thread to which this handler is attached ( int htis case the UI thread) . 该runnable将在连接该处理程序的线程上运行(包括UI线程)。 The time-base is uptimeMillis() . 时基是uptimeMillis() Time spent in deep sleep will add an additional delay to execution. 深度睡眠所花费的时间会增加执行时间。

Reference : Android API documentation 参考: Android API文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM