AudioRecord始终创建失败

时间:2014-08-12 04:16:56

标签: android audiorecord

我在使用AudioRecord for Android时遇到了问题。我已经在网上找到了关于它的内容,但我似乎无法进行良好的初始化。

我创建AudioRecord对象的代码如下:

    int bufferSize = AudioRecord.getMinBufferSize(44100, AudioFormat.CHANNEL_IN_MONO,
            AudioFormat.ENCODING_PCM_16BIT);
    if (bufferSize < 4096)
        bufferSize = 4096;

    mBuffer = new short[bufferSize];
    audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, 44100, AudioFormat.CHANNEL_IN_MONO,
            AudioFormat.ENCODING_PCM_16BIT, bufferSize);

我获得了正确的许可:

但每当我运行该应用时,都会显示此错误:

  

08-12 11:56:05.669:E / AudioRecord(10689):无法获得音频输入   记录来源1

     

08-12 11:56:05.669:E / AudioRecord-JNI(10689):   创建AudioRecord实例时出错:初始化检查失败。

     

08-12 11:56:05.669:E / AudioRecord-Java(10689):[   android.media.AudioRecord]初始化本机时的错误代码-20   AudioRecord对象。


1 个答案:

答案 0 :(得分:1)

所以这是你在android中录制音频文件所要做的。 首先,您必须为Androidmanifest.xml文件添加一些权限。应用程序需要具有写入外部存储的权限才能为Android录制音频

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />

接下来,您必须导入所需的类和函数

import android.app.Activity;
import android.widget.LinearLayout;
import android.os.Bundle;
import android.os.Environment;
import android.view.ViewGroup;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Context;
import android.util.Log;
import android.media.MediaRecorder;
import android.media.MediaPlayer;

import java.io.IOException;

创建Android.media.MediaRecorder的实例。

private void startRecording() {

 mRecorder = new MediaRecorder();

接下来,您必须设置音频输入的来源,在大多数情况下,您将使用Android设备的MIC为您的应用程序提供音频输入,以便在Android中录制音频文件。在这种情况下,您必须将音频源设置为:MediaRecorder.AudioSource.MIC

mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);

接下来,您必须为录像机文件定义输出格式。 Android支持多种音频格式。为此,您必须使用MediaRecorder.SetOutputFormat();功能。

 mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);

现在您必须命名一个文件,该文件将以您指定的格式保存您的录音。为此,您必须使用MediaRecorder.SetOutputfile()函数。

mRecorder.setOutputFile(mFileName);

现在使用MediaRecorder.setAudioEncoder()函数设置音频编码器。

mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

预备您的应用程序以录制音频输入调用MediaRecorder.prepare()

 mPlayer.prepare();
            mPlayer.start();
        } catch (IOException e) {
            Log.e(LOG_TAG, "prepare() failed");
        }
    }

现在一切都已准备就绪,您所要做的就是启动音频捕获过程。为此你必须这样做:MediaRecorder.start();

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

要停止录制,请调用MediaRecorder.stop() 最后,当您完成录制后,您必须通过调用MediaRecorder.release()来释放资源

mPlayer.release();

以下是记录音频然后播放的应用程序示例。

package com.android.audiorecordtest;

import android.app.Activity;
import android.widget.LinearLayout;
import android.os.Bundle;
import android.os.Environment;
import android.view.ViewGroup;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Context;
import android.util.Log;
import android.media.MediaRecorder;
import android.media.MediaPlayer;

import java.io.IOException;

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 PlayButton   mPlayButton = null;
    private MediaPlayer   mPlayer = null;

    private void onRecord(boolean start) {
        if (start) {
            startRecording();
        } else {
            stopRecording();
        }
    }

    private void onPlay(boolean start) {
        if (start) {
            startPlaying();
        } else {
            stopPlaying();
        }
    }

    private void startPlaying() {
        mPlayer = new MediaPlayer();
        try {
            mPlayer.setDataSource(mFileName);
            mPlayer.prepare();
            mPlayer.start();
        } catch (IOException e) {
            Log.e(LOG_TAG, "prepare() failed");
        }
    }

    private void stopPlaying() {
        mPlayer.release();
        mPlayer = null;
    }

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

    class PlayButton extends Button {
        boolean mStartPlaying = true;

        OnClickListener clicker = new OnClickListener() {
            public void onClick(View v) {
                onPlay(mStartPlaying);
                if (mStartPlaying) {
                    setText("Stop playing");
                } else {
                    setText("Start playing");
                }
                mStartPlaying = !mStartPlaying;
            }
        };

        public PlayButton(Context ctx) {
            super(ctx);
            setText("Start playing");
            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));
        mPlayButton = new PlayButton(this);
        ll.addView(mPlayButton,
            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;
        }

        if (mPlayer != null) {
            mPlayer.release();
            mPlayer = null;
        }
    }
}

或使用this惊人的教程