gpt4 book ai didi

android - AudioRecord 总是创建失败

转载 作者:行者123 更新时间:2023-11-30 02:44:45 32 4
gpt4 key购买 nike

我在使用 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): Could not get audio inputfor record source 1

08-12 11:56:05.669: E/AudioRecord-JNI(10689):Error creating AudioRecord instance: initialization check failed.

08-12 11:56:05.669: E/AudioRecord-Java(10689): [android.media.AudioRecord ] Error code -20 when initializing nativeAudioRecord object.


最佳答案

所以这是在 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很棒的教程

关于android - AudioRecord 总是创建失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25256074/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com