Dark Knight Rising
Dark Knight Rising

Reputation: 51

Need to kill and restart a thread

I am starting a thread when invoking a method to play an audio file.

The code runs ok the first time but when I call the play method again I need the thread to start as if it were being called the first time. I have tried to interrupt the thread and even stop it but nothing seems to work.

How can I properly restart the thread?

Here is some code to help explain.

Global variable

private Thread thread1;

Thread code:

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();

This code works fine and plays the track. It then resets the seekbar and awaits for the play method to be called again.

public void play()
{

    try 
    { 

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

}

Here is the setRunning method recommended to me.

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

If anyone know of a solution to this problem I would really appreciate it.

Upvotes: 0

Views: 2390

Answers (3)

Michał Majcherski
Michał Majcherski

Reputation: 236

When using extra boolean flag to controll thread's while loop don't forget to use volatile modifier on it:

     private volatile boolean running;

or put appropriate synchronization.

Apart from that, I'd think about using Thread.isInterrupted() method instead of the additional 'running' flag: http://docs.oracle.com/javase/6/docs/api/java/lang/Thread.html#isInterrupted()

here's why: https://stackoverflow.com/a/3196058/1350225

Upvotes: 0

Nick
Nick

Reputation: 1340

To restart your player you need to prepare again.

    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();
            }
        }
    }

Upvotes: 0

kgautron
kgautron

Reputation: 8263

Threads are not supposed to be stopped manually. You should use a boolean instead of true in your while loop, and put the boolean to false through a setter when you want to stop:

private boolean running;

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

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

Upvotes: 1

Related Questions