0

我在调用播放音频文件的方法时启动了一个线程。

代码第一次运行正常,但是当我再次调用 play 方法时,我需要启动线程,就像第一次调用它一样。我试图中断线程甚至停止它,但似乎没有任何效果。

如何正确重启线程?

这是一些帮助解释的代码。

全局变量

private Thread thread1;

线程代码:

thread1 = new Thread(new Runnable() 
    {
        @Override
         public void run() 
        {
             try {
             int i=0;

             final TextView timeDuration = (TextView) findViewById(R.id.timeDisplay);
             final SeekBar seekBar = (SeekBar)findViewById(R.id.seekBar1);
             while (running)
             {
                info();
                j = 0;
                while(i>0 && running)
                {


                while(j<duration && running && (playStatus.getValue() == "TRANSITIONING" ||         playStatus.getValue() == "PLAYING"))
                {

                seekBar.setMax((int) duration);
                seekBar.setProgress(0);

                runOnUiThread(new Runnable() 
                { 
                                    public void run() 
                                    {
                                        System.out.println("PLAYBACK STATUS: "+playStatus.getValue());
                                        timeDuration.setText(""+(j+1));
                                        seekBar.setProgress(j+1);
                                            if(j==(duration-1))
                                        {
                                            setRunning(false);
                                        }

                                    }
                });
                Thread.sleep(1 * 1000);
                j++;
                if(seekBar.getProgress() == seekBar.getMax())
                {

                runOnUiThread(new Runnable() 
                    { 
                                        public void run() 
                                        {
                                            playButton.setVisibility(View.VISIBLE);
                                            pauseButton.setVisibility(View.GONE);
                                            timeDuration.setText(""+"0");
                                            seekBar.setProgress(0);
                                            flag = false;
                                            System.out.println("J VALUE 1: = "+j);
                                            duration = 0;
                                            setRunning(false); 
                                        }
                    });
                }

                }
                }

                j = 0;
                        i++;
                        Thread.sleep(1 * 1000);
                    }

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
         }
    });

    play();

此代码工作正常并播放曲目。然后它重置搜索栏并等待再次调用播放方法。

public void play()
{

    try 
    { 

        thread1.start();
    } 
    catch(Exception e) 
    { 
            return; 
    }

}

这里是推荐给我的 setRunning 方法。

public void setRunning(boolean b)
{
      this.running = b;
}

如果有人知道这个问题的解决方案,我将不胜感激。

4

3 回答 3

1

不应手动停止线程。您应该使用布尔值而不是truewhile循环中,并false在您想停止时将布尔值放入 setter 中:

private boolean running;

@Override
public void run(){
  running = true;
  while(running) {...}
}

public void setRunning(boolean b){
  this.running = b;
}
于 2012-08-03T18:14:31.493 回答
0

要重新启动播放器,您需要再次准备。

    public void restartAudio() {
        Log.e(TAG, "restartAudio");
        if (mp.isPlaying()) {
            mp.seekTo(0);
        }else{
            mp.stop();
            try {
                mp.prepare();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
于 2012-08-06T13:29:09.593 回答
0

当使用额外的布尔标志来控制线程的 while 循环时,不要忘记在其上使用 volatile 修饰符:

     private volatile boolean running;

或放置适当的同步。

除此之外,我会考虑使用 Thread.isInterrupted() 方法而不是额外的“运行”标志: http ://docs.oracle.com/javase/6/docs/api/java/lang/Thread.html #isInterrupted()

这就是为什么: https ://stackoverflow.com/a/3196058/1350225

于 2012-08-14T20:21:53.403 回答