Sound Level not showing

94 Views Asked by At

What am i trying to do?

I am trying to make an app that records the ball bounces of a football (soccer ball). This is my first time writing an app, and first time using java. One of my steps is to record the sound and displaying the amplitude of the sound level. But right now it still shows 0.0 at my screen.

Code to show the sound level

package com.example.user.sportsballbounceapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import java.io.IOException;

public class Football1Test extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_football1_test);

        SoundMeter Sound=new SoundMeter();
        double myValue = Sound.getAmplitude();
        String message =
                myValue == -1 ?
                        "The value is invalid." :
                        "The value is " + myValue;
        TextView tv = (TextView) findViewById(R.id.textView2);
        tv.setText(message);
    }
}

Code to measure sound level

package com.example.user.sportsballbounceapp;

import android.media.MediaRecorder;

import java.io.IOException;

public class SoundMeter {

    private MediaRecorder mRecorder = null;

    public void start() throws IOException {
        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");
            mRecorder.prepare();
            mRecorder.start();
        }
    }

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

    public double getAmplitude() {
        if (mRecorder != null)
            return  mRecorder.getMaxAmplitude();
        else
            return 0;
    }
}

Result

The result is the same when testing on my phone as it is in the simulator (image is from the simulator)

image

enter image description here

Update

I also added permissions in the android manifest

<uses-permission android:name="android.permission.RECORD_AUDIO" />
1

There are 1 best solutions below

6
On

You need a listener to detect the changes in your recorder state. At the moment you are only setting the value of the TextView in onCreate but you are not observing anything.

What you can do is to run a Thread, and at every let's say, 3 seconds measure the max amplitude registered since the recorder is recording.

This is an example how:

final Handler handler = new Handler(); 
Runnable runnable = new Runnable() { 

@Override 
public void run() { 
    try{
        myTexView.setText(mRecorder.getMaxAmplitude());
    }
    catch (Exception e) {
        // TODO: handle exception
    }
    finally{
        //also call the same runnable to call it at regular interval
        handler.postDelayed(this, 3000); //every 3 seconds
    }
} 
}; 
handler.postDelayed(runnable, 3000);