记录特定的时间间隔

时间:2014-05-23 11:50:38

标签: android audio mediarecorder

我想使用MediaRecorder录制一秒钟的音频输入。我添加

时收到错误
  

handler.postDelayed(..)

这里是代码,任何人都可以告诉我如何解决它?我也尝试在单独的线程中执行此操作,但当我需要从其他类调用它时它会变得复杂。

    public class AudioRecordTest extends Activity
{
    private static final String LOG_TAG = "AudioRecordTest";

    private static String mFileName = null;

    private RecordButton mRecordButton = null;

    private MediaRecorder mRecorder = null;

    private final Handler handler=new Handler();

    private void onRecord(boolean start) {

        if (start) 
            startRecording();
        handler.postDelayed(new Runnable(){
            @Override
        public void run()
        {
            stopRecording();
        }

        },1000);
    }
    private void startRecording() {
        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mRecorder.setOutputFile(mFileName);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        try {
            mRecorder.prepare();
        } catch (IOException e) {
            Log.e(LOG_TAG, "prepare() failed");
        }
        mRecorder.start();
    }
    private void stopRecording() {
        mRecorder.stop();
        mRecorder.release();
        mRecorder = null;
    }
    class RecordButton extends Button {
        boolean mStartRecording = true;
        OnClickListener clicker = new OnClickListener() {
            public void onClick(View v) {
                onRecord(mStartRecording);
                if (mStartRecording) {
                    setText("Stop recording");
                } else {
                    setText("Start recording");
                }
                mStartRecording = !mStartRecording;
            }
        };
        public RecordButton(Context ctx) {
            super(ctx);
            setText("Start recording");
            setOnClickListener(clicker);
        }
    }

    public AudioRecordTest() {
        mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
        mFileName += "/audiorecordtest.3gp";
    }
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        LinearLayout ll = new LinearLayout(this);
        mRecordButton = new RecordButton(this);
        ll.addView(mRecordButton,
                new LinearLayout.LayoutParams(
                        ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT,
                        0));

        setContentView(ll);
    }
    @Override
    public void onPause() {
        super.onPause();
        if (mRecorder != null) {
            mRecorder.release();
            mRecorder = null;
        }
    }
}*

0 个答案:

没有答案