开发者

Android MediaRecorder getMaxAmplitude always returns 0 on LG Optimus

I am writing an app that basically just tests if we can get anything at all from the microphone.

It works perfectly on several android devices, but not on the LG O开发者_开发百科ptimus. Everytime I call MediaRecorder.getMaxAmplitude on the LG it returns 0.

The device is successfully recording, because I can listen to the recordings.


getMaxAmplitude returns the maximum amplitude since the last time you called it.

So, the first time you call it, it initializes itself (and so returns 0) the second time, it should return another value. According to the documentation :

getMaxAmplitude() returns the maximum absolute amplitude that was sampled since the last call to this method. Call this only after the setAudioSource().

Returns the maximum absolute amplitude measured since the last call, or 0 when called for the first time

Or, if you use it right, you have the same problem as I have. My code works on a galaxyTab 7(Froyo), but not 10.1 (Honeycomb).

EDIT : I repaired my problem (I hope it'll help you too). Make sure the first time you call getMaxAmplitude, in order to initialize it, you called start() first. I used:

recorder.prepare();
recorder.getMaxAmplitude();
recorder.start();
//listening to the user
int amplitude = recorder.getMaxAmplitude();

When it should have been :

recorder.prepare();
recorder.start();
recorder.getMaxAmplitude();
//listening to the user
int amplitude = recorder.getMaxAmplitude();

EDIT : This code still has flaws it seems. This code doesn't seems to work on S2, for instance. It'll return 0. But I call getMaxAmplitude() only twice, so if you need to update the amplitude every second for example, it may be ok.


A solution is to have the amplitude reading inside of a thread. You will see that from time to time you will have a value different than 0.0f.

private Runnable mPollTask = new Runnable() {
    public void run() {
            while(true){
           double amp = mSensor.getAmplitude();
           System.out.print("Amplitude: ");
           System.out.println(amp);
            }
    }
};


Below code worked for me, need to set setAudioSamplingRate and setAudioEncodingBitRate

MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
if (Build.VERSION.SDK_INT >= 10) {
recorder.setAudioSamplingRate(44100);
recorder.setAudioEncodingBitRate(96000);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
} else {
// older version of Android, use crappy sounding voice codec
recorder.setAudioSamplingRate(8000);
recorder.setAudioEncodingBitRate(12200);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
}
recorder.setOutputFile(file.getAbsolutePath());
try {
recorder.prepare();
} catch (IOException e) {
throw new RuntimeException(e);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜