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 8743683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:43:15+00:00 2026-06-13T11:43:15+00:00

Im having a problem with my metronome application. The application needs to loop when

  • 0

Im having a problem with my metronome application. The application needs to loop when the start button is pressed, and continue to loop until the stop button is pressed. Ive tried having the loop call methods, but it doesnt even listen to the button since it is stuck in the loop. My current code is.

public class MainActivity extends Activity {
    // constant
    final int MINUTE = 60000;

    // variables
    SeekBar seekbar1, seekbar2;
    EditText txtBPM1, txtBPM2;
    Spinner spnTop, spnBottom;
    int value1, value2;
    boolean playing = false;
    int tick;
    int beat;
    SoundPool sp;

    // loop variable
    int i = 0;
    int tickCount = 0;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
        tick = sp.load(this, R.raw.tick, 1);
        beat = sp.load(this, R.raw.beat, 1);

        final Spinner spnTop = (Spinner) findViewById(R.id.spnTop);
        // Create an ArrayAdapter using the string array and a default spinner
        // layout
        ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(
                this, R.array.times1, android.R.layout.simple_spinner_item);
        // Specify the layout to use when the list of choices appears
        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // Apply the adapter to the spinner
        spnTop.setAdapter(adapter1);
        spnTop.setSelection(2);

        final Spinner spnBottom = (Spinner) findViewById(R.id.spnBottom);
        // Create an ArrayAdapter using the string array and a default spinner
        // layout
        ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(
                this, R.array.times2, android.R.layout.simple_spinner_item);
        // Specify the layout to use when the list of choices appears
        adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // Apply the adapter to the spinner
        spnBottom.setAdapter(adapter2);

          ////////////////////////////////////////////////
         //Seekbar/EditText//////////////////////////////
        ////////////////////////////////////////////////
        seekbar1 = (SeekBar) findViewById(R.id.skbBPM1);
        txtBPM1 = (EditText) findViewById(R.id.txtBPM1);

        txtBPM1.setText("" + 120, TextView.BufferType.EDITABLE);

        seekbar1.setVerticalScrollbarPosition(value1);

        seekbar1.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
            public void onProgressChanged(SeekBar seekbar, int progress,
                    boolean fromUser) {
                value1 = progress + 30;
                txtBPM1.setText("" + value1, TextView.BufferType.EDITABLE);
            }

            @Override
            public void onStartTrackingTouch(SeekBar arg0) {
                // TODO Auto-generated method stub
                // stops metronome if playing
                i = 1;
            }

            @Override
            public void onStopTrackingTouch(SeekBar arg0) {
                // set the textbox to a new value
                txtBPM1.setText("" + value1, TextView.BufferType.EDITABLE);
            }
        });

        Button btnStart1 = (Button) findViewById(R.id.btnStart1);
        btnStart1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                value1 = Integer.parseInt(txtBPM1.getText().toString());
                if (value1 > 250 || value1 < 30) {
                    Toast.makeText(
                            getApplicationContext(),
                            "BPM value must be at least 30 and no more than 250.",
                            Toast.LENGTH_LONG).show();
                } else {
                    // set frequency and timers

                    int tickTime = MINUTE / value1;
                    int n = Integer.parseInt(spnBottom.getSelectedItem()
                            .toString());
                    int beat = Integer.parseInt(spnTop.getSelectedItem()
                            .toString());

                    tickTime = (tickTime * 4) / n;

                    // loop to play sound every specified incriment
                    for (i = 0; i > 0; i += 0)
                    {
                        tickCount++;
                        if (tickCount == beat) {
                            try
                            {
                                Thread.sleep(tickTime);
                            }
                            catch (InterruptedException e)
                            {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            sp.play(beat, 1, 1, 0, 0, 1);
                            tickCount = 0;
                            }
                        else
                        {
                            try
                            {
                                Thread.sleep(tickTime);
                            }
                            catch (InterruptedException e)
                            {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }//end catch
                            sp.play(tick, 1, 1, 0, 0, 1);
                            tickCount++;

                        }//end else
                    }//end loop
                }//end else
            }
        });

        Button btnStop1 = (Button) findViewById(R.id.btnStop1);
        btnStop1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (playing == true) {
                    i = 1;
                } else {
                    Toast.makeText(
                            getApplicationContext(),
                            "Metrenome is not playing. To begin playing press the Start button.",
                            Toast.LENGTH_LONG).show();
                }
            }
        });

        //////////////////////////////////////////////
        //Second spinner boxes/buttons////////////////
        //////////////////////////////////////////////
        /*
        seekbar2 = (SeekBar) findViewById(R.id.skbBPM2);
        txtBPM2 = (EditText) findViewById(R.id.txtBPM2);

        txtBPM2.setText("" + 120, TextView.BufferType.EDITABLE);

        value2 = Integer.parseInt(txtBPM2.getText().toString());

        seekbar2.setVerticalScrollbarPosition(value2);

        seekbar2.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
            public void onProgressChanged(SeekBar seekbar, int progress,
                    boolean fromUser) {
                value2 = progress + 30;
                txtBPM2.setText("" + value2, TextView.BufferType.EDITABLE);
            }

            @Override
            public void onStartTrackingTouch(SeekBar arg0) {
                // TODO Auto-generated method stub
                // Nothing happens
            }

            @Override
            public void onStopTrackingTouch(SeekBar arg0) {
                // set the textbox to a new value
                txtBPM2.setText("" + value2, TextView.BufferType.EDITABLE);
            }
        });
            */
    }// end onCreate
}// end class
  • 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-13T11:43:16+00:00Added an answer on June 13, 2026 at 11:43 am

    I’m pretty sure what you are doing wrong is starting a loop in UI thread. When you do a full-time action in your UI Thread – you block all buttons and they are not clickable any more. What you should do is starting AsyncTask or FutureTask (Async implementation could be easier) and cancel it with interruption on “Stop button”. In your loop you would have to check the Thread.getCurrentThread().isInterrupted() so it could stop asap.

    I will give you some pseudocode:

        onStartClickListener{
            onClick(){
                //make some checks (if (value1 > 250 || value1 < 30) etc..)
                myTask = new MyTask();
                myTask.execute();
            }
        }
    
        onStopClickListener{
            onClick(){
                myTask.cancel(true);
            }
        }
    
        class MyTask extends AsyncTask<Void,Void,Void>{
            doInBackground(){
                while(true){
                    // WE ARE IN DIFFERENT (non UI) thread in this method
    
                    // looping and playing sound or whatever else you want
                    // remember you cant do UI actions here
                    // when you want to update UI from here you need to do a mHandler = new Handler()
                    // in onCreate and do mHandler.post(new Runnable()) here
                    if (Thread.currentThread().isInterrupted()) break;
                }
            }
    
            onPostExecute(){
                // use it when you want to update UI when background task was finished
            }
    
            onCancelled(){
                // use it when you want to update UI when task was canceled. This is your case I think.
            }
        }
    

    Hope that helped 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I having problem in my application.. I am pressing back button in my third
Im having problem extending the life of my session. I tried //start sessions ini_set('session.gc-maxlifetime',
I am having problem in using this font in my Application. I have imported
Having problem with my application, I seem to be new in ios. My project
I having problem when I deploy an application on Windows Mobile Device .The device
I having problem managed thread parallel in console application. I am running 10 threads
I am having problem typing character glottal stop into a JTable, and only 0241
am having problem, my app doesn't seem to check the radio button based from
Im really having a problem with my image with button frame, What I want
im having problem with getting value of a variable from the ajax-call function getRequest

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.