[为了更好地理解Android中的音频记录和播放,我在android dev教程https://developer.android.com/guide/topics/media/mediarecorder#sample-code中实现了代码的克隆。>
开始录制时没有错误,但是,我还没有验证是否正在实际进行录制。然后,当我停止录制时,应用程序崩溃到上一个活动,并且在随后的尝试中,“应用程序崩溃”对话框弹出,并且该应用程序退出。
我在AndroidManifest中有<uses-permission android:name="android.permission.RECORD_AUDIO" />
private void onRecord(boolean start) { if (start) { startRecording(); } else { stopRecording(); // Line 53 // } } private void startRecording() { recorder = new MediaRecorder(); recorder.setAudioSource(MediaRecorder.AudioSource.MIC); recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); recorder.setOutputFile(fileName); System.out.println(fileName); recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); try { recorder.prepare(); } catch (IOException e) { Log.e(LOG_TAG, "prepare() failed"); //e.printStackTrace(); } } private void stopRecording() { recorder.stop(); // Line 98 // recorder.release(); recorder = null; } class RecordButton extends AppCompatButton { boolean mStartRecording = true; OnClickListener clicker = new OnClickListener() { public void onClick(View v) { onRecord(mStartRecording); if(mStartRecording) { setText(R.string.stop_recording); } else { setText(R.string.start_recording); } mStartRecording = !mStartRecording; } }; public RecordButton(Context ctx){ super(ctx); setText(R.string.start_recording); setOnClickListener(clicker); } }
Logcat输出:
2020-02-14 10:57:09.789 23619-23619/? E/Zygote: accessInfo : 1 2020-02-14 10:57:09.820 23619-23619/? E/.xxxxxx.xxxxxx: Unknown bits set in runtime_flags: 0x8000 2020-02-14 10:57:12.925 23619-23619/xx.xxxxxxx.xxxxxx.xxxxxxx E/MediaRecorder: stop called in an invalid state: 8 2020-02-14 10:57:12.927 23619-23619/xx.xxxxxxx.xxxxxx.xxxxxxx E/AndroidRuntime: FATAL EXCEPTION: main Process: xx.xxxxxxx.xxxxxx.xxxxxxx, PID: 23619 java.lang.IllegalStateException at android.media.MediaRecorder._stop(Native Method) at android.media.MediaRecorder.stop(MediaRecorder.java:1440) at xx.xxxxxxx.xxxxxx.xxxxxxx.RecordActivity.stopRecording(RecordActivity.java:98) at xx.xxxxxxx.xxxxxx.xxxxxxx.RecordActivity.onRecord(RecordActivity.java:53) at xx.xxxxxxx.xxxxxx.xxxxxxx.RecordActivity.access$000(RecordActivity.java:21) at xx.xxxxxxx.xxxxxx.xxxxxxx.RecordActivity$RecordButton$1.onClick(RecordActivity.java:108) at android.view.View.performClick(View.java:7866) at android.widget.TextView.performClick(TextView.java:14952) at android.view.View.performClickInternal(View.java:7839) at android.view.View.access$3600(View.java:886) at android.view.View$PerformClick.run(View.java:29363) at android.os.Handler.handleCallback(Handler.java:883) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loop(Looper.java:237) at android.app.ActivityThread.main(ActivityThread.java:7811) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1076)
如果需要,我可以提供任何其他相关代码或详细信息
编辑请求具有以下权限:
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); switch (requestCode){ case REQUEST_RECORD_AUDIO_PERMISSION: permissionToRecordAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED; break; } if (!permissionToRecordAccepted ) finish(); }
和onCreate()内:
ActivityCompat.requestPermissions(this, permissions, REQUEST_RECORD_AUDIO_PERMISSION);
编辑2 *我已验证正在创建录音文件,但大小为0 B
[为了更好地理解Android中的音频录制和播放,我在android dev教程https://developer.android.com/guide/topics/media / ...中实现了代码的克隆。
在下面的flowchart中,stop()
的有效状态是{Prepared, Started, Stopped, Paused, PlaybackCompleted}
。其他状态(空闲,初始化,错误)是NOT