MediaRecorder, getMaxAmplitude always returns 0

I developed this little MediaRecorder application. I have 3 buttons - Play

(to start recording), Pause

(to stop recording), Max

(to determine the maximum amplitude). I Googled to figure this out, but nothing worked for me.

The problem is, it always gives me 0 for getMaxAmplitude

.

Below is the code I am using. Can anyone please help?

public class MainActivity extends Activity {

    Button play,pause, max;

    String outputFile = Environment.getExternalStorageDirectory().
          getAbsolutePath() + "/myexampl.3gp";
    MediaRecorder mrecorder=new MediaRecorder();

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

        play = (Button)findViewById(R.id.button1);
        pause= (Button)findViewById(R.id.button2);
        max = (Button)findViewById(R.id.button3);

        mrecorder = new MediaRecorder();

        mrecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mrecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mrecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
        mrecorder.setOutputFile(outputFile);

        play.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                try {
                    mrecorder.prepare();
                } catch (IllegalStateException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                mrecorder.start();
                Toast.makeText(getApplicationContext(), "Starts Recording!", 
                    Toast.LENGTH_SHORT).show();
            }
        });

        pause.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                try {
                    mrecorder.stop();
                } catch (IllegalStateException e) {
                    e.printStackTrace();
                }

                mrecorder.release();
                Toast.makeText(getApplicationContext(), "Stops Recording!", 
                        Toast.LENGTH_SHORT).show();
            }
        });

        max.setOnClickListener(new OnClickListener() {
            int ampl=0;

            @Override
            public void onClick(View v) {

            try {
                ampl=mrecorder.getMaxAmplitude();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            }

            Toast.makeText(getApplicationContext(), String.valueOf(ampl), 
                    Toast.LENGTH_SHORT).show();
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

      

}

+3


source to share





All Articles