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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:30:48+00:00 2026-05-27T13:30:48+00:00

As the question says, here’s the code. What I want to do basically is

  • 0

As the question says, here’s the code.

What I want to do basically is I have a list of array, says “myArray”. This array contains stored time points in millisecond. Next thing is I want to call a method based on those time points. So, I need to run a time counter from 0 while the music is playing, compare the time counter with the time points in the array progressively (from first array index until finished).

The first method “timeCounter” is a method to start a time counter while a music is playing through MediaPlayer.

public void timeCounter(){
    Runnable myRunnable = new Runnable(){
        @Override
        public void run(){
            while(musicPlaying){
                startCounter(arrayPos);
            }
        }
    };
    Thread musicPlayer = new Thread(myRunnable);
    musicPlayer.start();
}

This is where the comparison of time counter and time in the array takes place.

public void startCounter(List<Long> array){
    elapsedTime = System.currentTimeMillis() - timeStart;
    if(elapsedTime == array.get(arrayPos)){
        arrayPos += 1;  //Continue comparing with the next number in the array
        Log.v("DEBUG", "TIME MATCHES!");
        if(arrayPos>= array.size()){
            arrayPos= 0;  //Prevent out of array index
        }
    }
}

I have the code running correctly and the result from the LogCat is correct.

Now the problem is sometimes the Thread is not running until the music finished playing (I have set onCompletionListener and set “musicPlaying” to false when music finished). The stopping point is totally random, sometimes when it’s just started, or around 50%, or even when it’s near to completion. Sometimes it doesn’t even start!
I’ve been trying to figure out why but I couldn’t find any info about Thread stopping halfway or such in anywhere. Thanks in advance! =)

  • 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-05-27T13:30:48+00:00Added an answer on May 27, 2026 at 1:30 pm

    I suspect your if(elapsedTime == array.get(arrayPos)) should be if(elapsedTime >= array.get(arrayPos)).

    You need to remember that the millisecond timer does not necessarily have a one millisecond tick. On Windows, for example, the returned value will only change every 15 ms or so. You may therefore completely miss a particular time and therefore never get past that entry in your list.

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

Sidebar

Related Questions

The question says it all basically. I want in a class MyClass to listen
Here's a link to my previous question on this same block of code with
The question says it all - I want to take a list object full
What the question says. In addition, is it possible to do this inline? Here
I have this homework question: Assume a Database table has 0 records at time
Here's one from the No question's too dumb department: Well, as the subject says:
Question says it all, really. My application is a time tracker. It's currently written
Question says it all. Some quick code examples of usage would be nice.. thanks!
here is the code for my question: class ICommon { public: virtual ICommon& operator=(const
Question says it all but here is an example: typedef struct mutable_t{ int count,

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.