Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8332113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:37:47+00:00 2026-06-09T02:37:47+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T02:37:49+00:00Added an answer on June 9, 2026 at 2:37 am

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm starting a thread from activity which is downloading a file. But in between,
I have some code for starting a thread on the .NET CF 2.0: ThreadStart
I have created a GUI for starting a Thread which does something very simple.
This is my thread: public void run() { Log.d(ConnectionThread,Starting Server Connection); try { while(isThereActivityRunning())
this is my thread: @Override public void run() { Log.d(ConnectionThread,Starting Server Connection); try {
My program does some network activity in a background thread. Before starting, it pops
When I'm creating a simple Windows form, is it starting in a new thread
Can I avoid third party code from creating new threads, starting new VMs, or
Starting to learn Canvas and have two classes so far (main one to call
Starting with API level 9, there's android:filterTouchesWhenObscured attribute and corresponding setFilterTouchesWhenObscured method on ViewGroup

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.