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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T19:39:00+00:00 2026-05-20T19:39:00+00:00

I’m trying to do a karaoke-like application . I want to display a word

  • 0

I’m trying to do a karaoke-like application. I want to display a word or words when a certain milliseconds come. For example:

1148 ms -> print “Nicholas “
1826 ms -> print “was “
2766 ms -> print “older “
…
*** ms -> display “*** “

Here’s my code:

package com.example.hellomedia;

import java.io.IOException;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;

public class HelloMedia extends Activity {

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

    final Handler mHandler = new Handler();
    final TextView tv = new TextView(this);

    tv.setText("Playing... ");
    setContentView(tv);

    final MediaPlayer mPlayer = MediaPlayer.create(this, R.raw.nicholas);




    final String words[] = {
            "Nicholas ",// 0
            "was ", // 1
            "older ",// 2
            "than ",// 3
            "sin ",// 4
            "and ",// 5
            "his ",// 6
            "beard ",// 7
            "could ",// 8
            "go ",// 9
            "no ",// 10
            "whiter. "// 11
    };



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

    mPlayer.start();

    mHandler.post(new Runnable(){
        public void run(){ 

            //tv.setText(" [ " + mPlayer.getCurrentPosition() + " ] ");
            if( mPlayer.getCurrentPosition() == 1148 ){//0
                tv.append(words[0]);
            }
            if( mPlayer.getCurrentPosition() == 1826 ){//1
                tv.append(words[1]);
            }
            if( mPlayer.getCurrentPosition() == 2766 ){//2
                tv.append(words[2]);
            }
            mHandler.postDelayed(this, 1);
        }    
    });


}

}

When I run this, no word(s) from the array is being printed.

I’m new to android dev. Many thanks in advance. 🙂


Thanks for all your responses @Matthew Willis, @MarvinLabs, and @Bill Mote. I came up exactly with what I needed. My code goes something like this:

package com.example.hellomedia;

import java.io.IOException;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;

public class HelloMedia extends Activity {

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

    final Handler mHandler = new Handler();
    final TextView tv = new TextView(this);

    tv.setText("Playing1... ");
    setContentView(tv);

    final MediaPlayer mPlayer = MediaPlayer.create(this, R.raw.nicholas);

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

    mPlayer.start();


    final String words[] = {
            "Nicholas ",// 0
            "was ", // 1
            "older ",// 2
            "than ",// 3
            "sin ",// 4
            "and ",// 5
            "his ",// 6
            "beard ",// 7
            "could ",// 8
            "go ",// 9
            "no ",// 10
            "whiter. "// 11
    };

    final long startEndTime[][]={
            {   //start time
                1148,// 0,0
                1826, // 0,1
                2766,// 0,2
                3079,// 0,3
                3549,// 0,4
                4540,// 0,5
                4697,// 0,6
                4801,// 0,7
                5114,// 0,8
                5323,// 0,9
                5532,// 0,10
                5845// 0,11
            },
            {   //end time
                1357,// 1,0
                2192, // 1,1
                3027,// 1,2
                3183,// 1,3
                3966,// 1,4
                4645,// 1,5
                4749,// 1,6
                4958,// 1,7
                5219,// 1,8
                5427,// 1,9
                5740,// 1,10
                6210// 1,11
            }

        };

    mHandler.post(new Runnable(){

        public void run(){ 
            final long currentPos = mPlayer.getCurrentPosition();

            int x = 0;

            while( x < 12){
                if( currentPos > startEndTime[0][x] && currentPos < startEndTime[1][x] ){//0
                    tv.append(words[x]);
                    words[x]="";
                }
                x++;
            }

            mHandler.postDelayed(this, 1);
        }    
    });


}
}
  • 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-20T19:39:01+00:00Added an answer on May 20, 2026 at 7:39 pm

    You are not guaranted that your message handler will be called exactly each millisecond. You must give it some slack:

     mHandler = new Handler() {
            public void handleMessage(Message msg){     
                final long currentPos = mPlayer.getCurrentPosition();
    
                if (currentPos > 1100 && currentPos < 1300) {
                    tv.append(words[0]);
                } else if (currentPos > 1300 && currentPos < 1400) {
                    tv.append(words[1]);
                } 
    
                mHandler.sendEmptyMessageDelayed(0, 1);
            }
        };
    
    
    mHandler.sendEmptyMessage(0);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to run a str_replace or preg_replace which looks for certain words
I am trying to understand how to use SyndicationItem to display feed which is
I want to count how many characters a certain string has in PHP, but
I am trying to render a haml file in a javascript response like so:
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
i want to parse a xhtml file and display in UITableView. what is the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
Basically, what I'm trying to create is a page of div tags, each has

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.