我正在尝试使用麦克风录制声音。该代码完美适用于 Samsung Note 2(N7100)但是我在某些设备上收到了一些错误。这些错误的意思是什么?我的代码在这里。提前谢谢。
private void startRecording() {
CharSequence now = android.text.format.DateFormat.format("dd-MM-yyyy HH:mm:ss", new Date());
String outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "The Looppad"
+ File.separator + now + ".3gp";
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mRecorder.setAudioSamplingRate(44100);
mRecorder.setAudioEncodingBitRate(96000);
mRecorder.setOutputFile(outputFile);
try {
mRecorder.prepare();
} catch (IllegalStateException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mRecorder.start();
record.setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN);
isRecording = true;
}
错误:
一般移动Android 4.2上的错误
java.lang.IllegalStateException
at android.media.MediaRecorder.start(Native Method)
at org.uusoftware.thelaunchpadhouse.ActivityFutureHouse.m(Unknown Source)
at org.uusoftware.thelaunchpadhouse.ActivityFutureHouse.onOptionsItemSelected(Unknown Source)
at android.app.Activity.onMenuItemSelected(Activity.java:2566)
at android.support.v4.b.ab.onMenuItemSelected(Unknown Source)
at android.support.v7.a.ag.onMenuItemSelected(Unknown Source)
at android.support.v7.view.n.onMenuItemSelected(Unknown Source)
at android.support.v7.a.as.a(Unknown Source)
at android.support.v7.view.menu.i.a(Unknown Source)
at android.support.v7.view.menu.m.b(Unknown Source)
at android.support.v7.view.menu.i.a(Unknown Source)
at android.support.v7.view.menu.i.a(Unknown Source)
at android.support.v7.widget.ActionMenuView.a(Unknown Source)
at android.support.v7.view.menu.ActionMenuItemView.onClick(Unknown Source)
at android.view.View.performClick(View.java:4211)
at android.view.View$PerformClick.run(View.java:17446)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5336)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
LG G2 Android 4.4上的错误
java.lang.IllegalStateException
at android.media.MediaRecorder.native_start(Native Method)
at android.media.MediaRecorder.start(MediaRecorder.java:758)
at org.uusoftware.thelaunchpadhouse.ActivityTechnoHouse.m(Unknown Source)
at org.uusoftware.thelaunchpadhouse.ActivityTechnoHouse.onOptionsItemSelected(Unknown Source)
at android.app.Activity.onMenuItemSelected(Activity.java:2644)
at android.support.v4.b.ab.onMenuItemSelected(Unknown Source)
at android.support.v7.a.ag.onMenuItemSelected(Unknown Source)
at android.support.v7.view.n.onMenuItemSelected(Unknown Source)
at android.support.v7.a.as.a(Unknown Source)
at android.support.v7.view.menu.i.a(Unknown Source)
at android.support.v7.view.menu.m.b(Unknown Source)
at android.support.v7.view.menu.i.a(Unknown Source)
at android.support.v7.view.menu.i.a(Unknown Source)
at android.support.v7.widget.ActionMenuView.a(Unknown Source)
at android.support.v7.view.menu.ActionMenuItemView.onClick(Unknown Source)
at android.view.View.performClick(View.java:4442)
at android.view.View$PerformClick.run(View.java:18473)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5105)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
这些代码解决了我的问题:
private void startRecording() {
isRecording = true;
RECORDER_SAMPLERATE = getMaxSupportedSampleRate();
bufferSize = AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE, AudioFormat.CHANNEL_IN_STEREO,
AudioFormat.ENCODING_PCM_16BIT);
recorder = new AudioRecord(MediaRecorder.AudioSource.VOICE_RECOGNITION, RECORDER_SAMPLERATE,
AudioFormat.CHANNEL_IN_STEREO, AudioFormat.ENCODING_PCM_16BIT, bufferSize);
if (android.os.Build.VERSION.SDK_INT >= 16) {
NoiseSuppressor.create(recorder.getAudioSessionId());
AutomaticGainControl.create(recorder.getAudioSessionId());
AcousticEchoCanceler.create(recorder.getAudioSessionId());
} else {
am = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
am.setParameters("noise_suppression=auto");
}
int i = recorder.getState();
if (i == 1) {
recorder.startRecording();
recordingThread = new Thread(new Runnable() {
@Override
public void run() {
writeAudioDataToFile();
}
}, "AudioRecorder Thread");
recordingThread.start();
} else {
System.out.println(i);
}
record.setColorFilter(Color.BLACK, PorterDuff.Mode.SRC_IN);
}
private int getMaxSupportedSampleRate() {
final int validSampleRates[] = new int[]{48000, 44100, 22050, 11025, 8000};
for (int i = 0; i < validSampleRates.length; i++) {
int result = AudioRecord.getMinBufferSize(validSampleRates[i], AudioFormat.CHANNEL_IN_STEREO,
AudioFormat.ENCODING_PCM_16BIT);
if (result != AudioRecord.ERROR && result != AudioRecord.ERROR_BAD_VALUE && result > 0) {
return validSampleRates[i];
}
}
return -1;
}
private void writeAudioDataToFile() {
byte data[] = new byte[bufferSize];
String filename = getTempFilename();
FileOutputStream os = null;
int read = 0;
try {
os = new FileOutputStream(filename);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (null != os) {
while (isRecording) {
read = recorder.read(data, 0, bufferSize);
if (AudioRecord.ERROR_INVALID_OPERATION != read) {
try {
os.write(data);
} catch (IOException e) {
e.printStackTrace();
}
}
}
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
您可以在Github
上看到完整代码