0

我正在尝试使用录音机,但我收到非法参数异常,指出录音机未初始化。

我的代码就像这里显示的一样

private static final int RECORDER_SAMPLERATE = 44100;
private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_STEREO;
private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;

recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,RECORDER_SAMPLERATE,  RECORDER_CHANNELS,RECORDER_AUDIO_ENCODING, bufferSize);                        
recorder.startRecording();

我看到了另一个似乎对某些人有用但对我 不起作用的答案 AudioRecord 对象未初始化

4

2 回答 2

0

试试这个:

private MediaRecorder mRecorder = null;
private void startRecording() {
        String fileSaveName = generateNameForAudioFile();
        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mRecorder.setOutputFile(fileSaveName);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);

        try {
            mRecorder.prepare();
        } catch (IOException e) {
            Log.e(LOG_TAG, "prepare() failed");
        }

        mRecorder.start();
    }



private void stopRecording() {
        startRecording.setEnabled(true);
        try {
            if (mRecorder != null) {
                mRecorder.stop();
                mRecorder.release();
                mRecorder = null;
            }
        } catch (Exception e) {
        }

    }

public String generateNameForAudioFile() {

        String audioName = GetrandFilename();
        mFileName = Environment.getExternalStorageDirectory().getPath() + "/"
                + audioName + "myaudio" + ".3gp";

        );
        return mFileName;
    }



@Override
    public void onPause() {
        super.onPause();

            try {
                if (mRecorder != null) {
                    mRecorder.stop();
                    mRecorder.release();
                    mRecorder = null;
                }
            } catch (Exception e) {
            }
              }

让我知道这篇文章是否有帮助。

于 2012-07-27T11:24:50.007 回答
0

我实际上是在做愚蠢的事情,我在应用程序标签内给予权限更正了它,它工作正常感谢大家的时间和支持

于 2012-07-28T10:13:59.737 回答