0

我是 Java 新手,我的英语也不是最好的。我想编写一个测量记录幅度的应用程序。该应用程序的工作原理是幅度始终为 0。我现在已经阅读了几个网站,但没有找到答案。如果有人帮助我,我会很高兴。

package com.example.volumeswitcher;

import java.io.IOException;

import android.app.Activity;
import android.media.MediaRecorder;
import android.media.MediaRecorder.OnInfoListener;
import android.os.Bundle;
import android.os.Environment;
import android.os.Vibrator;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnInfoListener {
    WindowManager.LayoutParams lp;
    Button messe;
    MediaRecorder micro;
    int ampli;
    TextView tv1;
    TextView tv2;
    String amplitext;
    Vibrator vibr;
    Environment e;
    String mFileName;
    int i;
    Boolean recording;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        Window w = getWindow();
        w.setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN, WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
        setContentView(R.layout.activity_main);
        micro = null;
    }

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


    public void onBtnClicked(View v) {
            startRecording();
        }

    protected void startRecording() {
        if (micro == null){
            i ++;
            mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
            mFileName += "/audiorecord"+ i +".3gp";
            micro = new MediaRecorder();

            micro.setAudioSource(MediaRecorder.AudioSource.MIC);
            micro.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            micro.setOutputFile(mFileName);
            micro.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            micro.setMaxDuration(2000);

              try {

                micro.prepare();
            } catch (IllegalStateException e) {
                Toast.makeText(getApplicationContext(), "IllegalStateException called", Toast.LENGTH_LONG).show();


            } catch (IOException e) {
                Toast.makeText(getApplicationContext(), "prepare() failed", Toast.LENGTH_LONG).show();

            }

              micro.start();
              micro.getMaxAmplitude();
              ampli = micro.getMaxAmplitude();
              tv1 = (TextView) findViewById(R.id.textView1);
              tv1.setText(Integer.toString(ampli));

        }

          micro.setOnInfoListener(new OnInfoListener() {
            @Override
            public void onInfo(MediaRecorder mr, int what, int extra) {
                if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {
                    micro.stop();
                    recording = false;
                    micro.release();
                    micro = null;
                }
            }
        });
    }

    @Override
    public void onInfo(MediaRecorder mr, int what, int extra) {
        // TODO Auto-generated method stub

    }
}

谢谢您的帮助!

4

1 回答 1

0

我有同样的问题,奇怪的是,如果你在尝试获得最大幅度时将时间间隔更改为大于一秒,它可以正常工作,但我不知道是什么原因造成的......

于 2013-12-29T18:53:41.240 回答