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

  • Home
  • SEARCH
  • 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 8911691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:00:44+00:00 2026-06-15T04:00:44+00:00

What I want to do is run a method that will play a random

  • 0

What I want to do is run a method that will play a random sound from my assets while a ToggleButton is On. And then stop doing that when the ToggleButton is Off. The problem is that as currently constructed, my code locks up with UI thread. I thought I was using AsyncTask correctly, but apparently not.

Can you suggest either a fix for this or a better method to accomplish what I want?

    public void onToggleClicked(View view) {
    // Is the toggle on?
    boolean on = ((ToggleButton) view).isChecked();



    while (on) {

        AssetManager assetMgr = this.getAssets();
        try {
            String[] filename = assetMgr.list("sounds");
            Random r = new Random();
            String fn = filename[r.nextInt(filename.length)];
            if (MyService.mp.isPlaying()) {

            }
            else {

                new PlayShuffleMode().execute(fn);

        }


        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

And here is the AsyncTask method:

private class PlayShuffleMode extends AsyncTask<String, Void, Void> {

    @Override
    protected Void doInBackground(String... fn) {

        mService.playfav(fn[0]);
        return null;
    }
}
  • 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-15T04:00:45+00:00Added an answer on June 15, 2026 at 4:00 am

    The problem is your while loop. The while loop will loop continuously on the main thread until you toggle the button, but since the while loop is on the main thread, your UI will get locked up. So you must figure out a way to move the while loop into your AsyncTask.

    One suggestion: Instead of having the AsyncTask inside of the while loop, why not do the inverse and have the while loop in the AsycTask? Something like…

    private playShuffle;
    
    public void onToggleClicked(View view) {
      ToggleButton tb = ((ToggleButton) view);
      tb.setChecked(!tb.isChecked()); // Toggle the state.
    
      if(tb.isChecked()) {
        playShuffle = new PlayShuffleMode();
        playShuffle.execute();
      }
      else {
        playShuffle.cancel();
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a method that run multiple threads and I want before
I just want that my program or method should run at specific date and
I'm trying to create an object with a run method that will be wrapped
Is it possible to run a method that will return the name of the
Question Is there a way to have a method that will always run anytime
On page ready I want to run a function that will get the css
Suppose I want to run the following method foo() once every hour in Grails:
I want to automatically run a class method defined in a base class on
I want to run SearchResultByOrderNumber(string orderNumber) method in Foreach with multithreading. There are ten
I want to schedulers background service to run a scan method. so the service

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.