1

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" />
RubenDefour
  • 31
  • 1
  • 10

1 Answers1

0

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); 
Ricardo
  • 9,136
  • 3
  • 29
  • 35
  • This might be a stupid question but how do I code that listener? – RubenDefour May 07 '18 at 10:24
  • I tried it still no difference. Also tried triggering it when pressing a button and same result. I changed the line: public double getAmplitude() { if (mRecorder != null) return mRecorder.getMaxAmplitude(); else return 0; } To public double getAmplitude() { if (mRecorder != null) return mRecorder.getMaxAmplitude(); else return 120; } To see if the recorder actually does something and it the result is that it changed the value to 120 – RubenDefour May 07 '18 at 13:34
  • Are you calling start? you need to call SoundMeter.start(); inside onCreate – Ricardo May 07 '18 at 13:50
  • When i do the app crashes on creating the activity – RubenDefour May 07 '18 at 14:00
  • Well, that is a different problem. Debug your code and find out what the problem is – Ricardo May 07 '18 at 14:05
  • It crashes on the line Sound.start(); and uin the soundmeter.java file on mediaRecorder.start(); – RubenDefour May 07 '18 at 14:10