2

我正在尝试编写一个实时读取麦克风数据的程序。

到目前为止,这是我的主要课程:

package com.example.mictest;

import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.media.MediaRecorder;
import android.media.MediaRecorder.AudioSource;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
    //variables
    private boolean isRecording = false;
    private int audioSource = MediaRecorder.AudioSource.MIC;
    private int samplingRate = 44100; /* in Hz*/
    private int channelConfig = AudioFormat.CHANNEL_IN_MONO;
    private int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
    private int bufferSize = AudioRecord.getMinBufferSize(samplingRate, channelConfig, audioFormat);
    private int sampleNumBits = 16;
    private int numChannels = 1;
    private byte[] data;

    private TextView TV;



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

        TV = (TextView) findViewById(R.id.textView1);

        AudioRecord recorder = new AudioRecord(AudioSource.MIC, samplingRate, channelConfig, audioFormat, bufferSize);
        TV.setText(Integer.toString(recorder.getState()));
        recorder.release();
    }

    @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;
    }

}

我已经在初始化 AudioRecord 时遇到了问题,getState() 的值为零。日志猫:

09-24 18:01:07.139: E/AudioRecord(2530): AudioFlinger could not create record track, status: -1
09-24 18:01:07.139: E/AudioRecord-JNI(2530): Error creating AudioRecord instance: initialization check failed.
09-24 18:01:07.139: E/AudioRecord-Java(2530): [ android.media.AudioRecord ] Error code -20 when initializing native AudioRecord object.

我已经在清单文件中设置了 RECORD_AUDIO 的权限。我有一个带有 API 版本 7 的三星 Galaxy Spica。

有谁知道为什么我不能让它工作?

4

2 回答 2

0

试试这个方法

private MediaRecorder mRecorder = null;


private void startRecording() {

        try {
            mRecorder = new MediaRecorder();
            mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            mFileName = getRecordDefaultFileName();
            mRecorder.setOutputFile(mFileName);
            mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

            try {
                mRecorder.prepare();
            } catch (IOException e) {
                System.out.println("prepare() failed");
            }

            mRecorder.start();
        } catch (Exception e) {

            return;
        }

    }

    /**
     * This method used to get record default file name.
     * 
     * @return represented {@link String}
     */
    private String getRecordDefaultFileName() {
        String fileName;
        File wallpaperDirectory = new File(Environment
                .getExternalStorageDirectory().getAbsolutePath()
                + "/"
                + "MyRecordings" + "/");
        if (!wallpaperDirectory.exists()) {
            wallpaperDirectory.mkdirs();
        }
        if (wallpaperDirectory.listFiles() != null) {
            fileName = "record" + wallpaperDirectory.listFiles().length;
        } else {
            fileName = "record" + 1;
        }

        return wallpaperDirectory.getAbsolutePath() + File.separator + ""
                + fileName + "" + ".3gp";
    }

    private void stopRecording() {
        try {
            if (mRecorder != null) {

                try {
                    mRecorder.stop();
                    mRecorder.release();
                } catch (Throwable e) {
                }
                mRecorder = null;
            }
        } catch (Exception e) {
        }
    }
于 2013-09-25T07:01:52.413 回答
-2

使用下面的代码并尝试:

private MediaRecorder mMediaRecorder = null;
private void startSpyRecording() {

        mMediaRecorder = new MediaRecorder();
        mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
        mMediaRecorder.setOutputFile("/sdcard/filenamevideo.mp4");

        try {
            mMediaRecorder.prepare();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        mMediaRecorder.start();
        status = true;

    }

    public void stopMediaRecorder() {

        mMediaRecorder.stop();
        mMediaRecorder.reset();
        mMediaRecorder.release();
    }

Put below permissions in your manifest.

<uses-permission android:name="android.permission.RECORD_AUDIO" >
    </uses-permission>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
    </uses-permission>
于 2013-09-25T06:44:20.470 回答