我希望我的程序录制音频10秒然后停止并将我的记录存储在文件存储中,一切正常在我的Nexus 4和Galaxy S 5上运行,但是当我在Galaxy S3中测试时它会破坏并引发错误< / p>
10-02 02:13:44.942 1279-1279/com.taptester.tappapp E/AudioCaptureDemo﹕ prepare() failed
10-02 02:13:44.942 1279-1279/com.taptester.tappapp E/MediaRecorder﹕ start called in an invalid state: 4
10-02 02:13:44.942 1279-1279/com.taptester.tappapp D/AndroidRuntime﹕ Shutting down VM
10-02 02:13:44.942 1279-1279/com.taptester.tappapp W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb1a12ba8)
10-02 02:13:44.952 1279-1279/com.taptester.tappapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.taptester.tappapp, PID: 1279
java.lang.IllegalStateException
at android.media.MediaRecorder.start(Native Method)
at com.taptester.tappapp.MainActivity.startRecording(MainActivity.java:203)
at com.taptester.tappapp.MainActivity.access$300(MainActivity.java:62)
at com.taptester.tappapp.MainActivity$6.onFinish(MainActivity.java:825)
at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:118)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
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:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
首先我认为错误在文件名中,我是这样声明的:
public MainActivity() {
mFileName = Environment.getExternalStorageDirectory() + File.separator
+ Environment.DIRECTORY_DCIM + File.separator + "MyMemo.3gp";
//Environment.getExternalStorageDirectory().getAbsolutePath();
//mFileName += "/MyMemo.3gp";
}
然后我做这样的记录:
private void startRecording() {
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
mRecorder.setOutputFile(mFileName);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
try {
mRecorder.prepare();
mRecorder.start();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
}
然后我打电话给&#34; StartRecording&#34;像这样的方法:
else if(command.equals("2")) {
startRecording();
Toast.makeText(getApplicationContext(), "Start recording...",
Toast.LENGTH_SHORT).show();
CountDownTimer start = new CountDownTimer(timer, 1000) {
@Override
public void onTick(long l) {
Toast.makeText(getApplicationContext(), "Recording!!!",
Toast.LENGTH_SHORT).show();
}
@Override
public void onFinish() {
stopRecording();
}
}.start();
答案 0 :(得分:1)
这是导致非法国家例外被抛出的原因。
将格式和编码器更改为s3设备支持的文件格式和编码格式。
首先,将您的编码设置更改为默认设置,如下所示,并尝试它是否有效。 mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);