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
Update
I also added permissions in the android manifest
<uses-permission android:name="android.permission.RECORD_AUDIO" />