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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:07:54+00:00 2026-06-07T02:07:54+00:00

Currently my Code is detecting voice intensity (using media recorder) i want to change

  • 0

Currently my Code is detecting voice intensity (using media recorder) i want to change background color to white when there is no voice , when user speak then background color must b light or dark according to voice intensity

here is my code im having problem to make color light and dark according to voice intensity .

final Runnable updater = new Runnable(){

    public void run(){          
        updateTv();
        TextView tv = (TextView)findViewById(R.id.status);
        int tvStatus=  Integer.parseInt(tv.getText().toString());
        if(tvStatus > 1000)
            updateBackground();
        else
            mScreen.setBackgroundColor(Color.WHITE);
    };
};

final Handler mHandler = new Handler();

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);
    mStatusView = (TextView) findViewById(R.id.status);
    mScreen = (LinearLayout) findViewById(R.id.myScreen);

    if (runner ==  null)
    { 
        runner = new Thread(){
            public void run()
            {
                while (runner != null)
                {


                    try
                    { 
                       Thread.sleep(1000);
                        Log.i("Noise", "Tock");
                    } catch (InterruptedException e) { };
                    mHandler.post(updater);
                }
            }
        };
        runner.start();
        Log.d("Noise", "start runner()");
    }
}

private void updateBackground()
{

     int ampl  =(int)getAmplitude();
    int color;
    Random rnd = new Random(); 

 mScreen.setBackgroundColor(

         color );

}
public void onResume()
{
    super.onResume();
    startRecorder();
}

public void onPause()
{
    super.onPause();
    stopRecorder();
}

public void startRecorder(){
    if (mRecorder == null)
    {
        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mRecorder.setOutputFile("/dev/null"); 
        try
        {           
            mRecorder.prepare();
        }catch (java.io.IOException ioe) {
           android.util.Log.e("[Monkey]", "IOException: " + android.util.Log.getStackTraceString(ioe));

        }catch (java.lang.SecurityException e) {
            android.util.Log.e("[Monkey]", "SecurityException: " + android.util.Log.getStackTraceString(e));
        }
        try
        {           
            mRecorder.start();
        }catch (java.lang.SecurityException e) {
            android.util.Log.e("[Monkey]", "SecurityException: " + android.util.Log.getStackTraceString(e));
        }

        //mEMA = 0.0;
    }

}
public void stopRecorder() {
    if (mRecorder != null) {
        mRecorder.stop();       
        mRecorder.release();
        mRecorder = null;


    }
}

public void updateTv(){
    mStatusView.setText(Integer.toString((getAmplitude())));
}
public double soundDb(double ampl){
    return  20 * Math.log10(getAmplitudeEMA() / ampl);
}
public int getAmplitude() {
    if (mRecorder != null)
        return  (mRecorder.getMaxAmplitude());
    else
        return 0;

}
  • 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-07T02:07:56+00:00Added an answer on June 7, 2026 at 2:07 am

    Your current implementation of updateBackground() uses color without initializing it:

    private void updateBackground() {
        int ampl = (int) getAmplitude();
        int color;
        Random rnd = new Random(); 
    
        mScreen.setBackgroundColor(color);
    }
    

    If the minimum amplitude is 0 and the maximum amplitude is MAX_AMPLITUDE, and if you want white to represent minimum amplitude, and black, maximum amplitude, then something like this should do the trick:

    private static final int MAX_RGB = 255;
    private static final int MAX_AMPLITUDE = 32767;
    
    private void updateBackground() {
        float amplF = (float) getAmplitude();
        int ampl = MAX_RGB - (int) (amplF / MAX_AMPLITUDE * MAX_RGB);
    
        mScreen.setBackgroundColor(Color.rgb(ampl, ampl, ampl));
    }
    

    If you find that the highest amplitude values you see in practice are significantly lower than 32767, you can account for this with:

    private static final int MAX_RGB = 255;
    private static final int int MAX_AMPLITUDE = 1500; // Set to some reasonable value
    
    private void updateBackground() {
        int actual = getAmplitude();
    
        if (actual > MAX_AMPLITUDE)
            actual = MAX_AMPLITUDE;
    
        float amplF = (float) actual;
        int ampl = MAX_RGB - (int) (amplF / MAX_AMPLITUDE * MAX_RGB);
    
        mScreen.setBackgroundColor(Color.rgb(ampl, ampl, ampl));
    }
    

    If you do that, it would probably be a good idea to make MAX_AMPLITUDE no longer a constant, and make it configurable by offering a “calibrate” option, where users can make whatever they consider to be a loud noise.

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

Sidebar

Related Questions

Using a JEditorPane, is there any way to tell if the caret is currently
Using a hashchange event I'm detecting when a user clicks the back button in
I am currently looking at some code for PHP detection of mobiles, which is
Currently my code below works fine but it's a bit of overkill. In my
Currently my code loads a TabWidget with 4 tabs. The first tab points to
Currently our code uses a for-loop for filling a buffer holding a Unicode string
My code currently looks like this: private Foo myFoo; public Foo CurrentFoo { get
My code currently looks like this: <div style=position: fixed; width: 35.25%; height: 6.75%; left:
I currently have code that does the following: private final static ExecutorService pool =
I currently have code in objective C that can pull out an integer's most

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.