MediaRecorder Error: setAudioSource in invalid state(4)
MediaRecorder Error: setAudioSource in invalid state(4)
我一直在尝试让自定义相机屏幕正常工作,但出于某种原因,以下代码似乎无法正常工作。我最终得到一个 RuntimeException
,这是由一个错误引起的:setAudioSource called in an invalid state(4)
.
以下是有问题的代码:
Preview.getRecorderInstance().setVideoSource(MediaRecorder.VideoSource.CAMERA);
Preview.getRecorderInstance().setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
Preview.getRecorderInstance().setAudioSource(MediaRecorder.AudioSource.MIC);
Preview.getRecorderInstance().setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
Preview.getRecorderInstance().setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
Preview.getRecorderInstance().setOutputFile(Environment.getExternalStorageDirectory().getAbsolutePath()
+ "/test" + System.currentTimeMillis() + ".mp4"
);
Preview.getRecorderInstance().prepare();
Preview.getRecorderInstance().start();
Preview.getRecorderInstance()
获取绑定到 Preview
class 的单例媒体记录器(它是 SurfaceView
的子class,旨在显示相机预览).
我的权限:
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
对于这方面的任何帮助,我将不胜感激,因为我在这项工作中一无所获,而且我已经在 Whosebug 上查看过类似的问题。阅读回复后我无法解决问题。
配置 MediaRecorder
所需的语句顺序很棘手。 documentation 声明必须在 setOutputFormat()
之前调用 setAudioSource()
。翻转语句的顺序是这样的:
Preview.getRecorderInstance().setAudioSource(MediaRecorder.AudioSource.MIC);
Preview.getRecorderInstance().setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
关注MediaRecorder文档官方即可。您的代码可能未遵循说明。函数调用顺序一定要正确
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.setOutputFile(_path);
我一直在尝试让自定义相机屏幕正常工作,但出于某种原因,以下代码似乎无法正常工作。我最终得到一个 RuntimeException
,这是由一个错误引起的:setAudioSource called in an invalid state(4)
.
以下是有问题的代码:
Preview.getRecorderInstance().setVideoSource(MediaRecorder.VideoSource.CAMERA);
Preview.getRecorderInstance().setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
Preview.getRecorderInstance().setAudioSource(MediaRecorder.AudioSource.MIC);
Preview.getRecorderInstance().setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
Preview.getRecorderInstance().setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
Preview.getRecorderInstance().setOutputFile(Environment.getExternalStorageDirectory().getAbsolutePath()
+ "/test" + System.currentTimeMillis() + ".mp4"
);
Preview.getRecorderInstance().prepare();
Preview.getRecorderInstance().start();
Preview.getRecorderInstance()
获取绑定到 Preview
class 的单例媒体记录器(它是 SurfaceView
的子class,旨在显示相机预览).
我的权限:
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
对于这方面的任何帮助,我将不胜感激,因为我在这项工作中一无所获,而且我已经在 Whosebug 上查看过类似的问题。阅读回复后我无法解决问题。
配置 MediaRecorder
所需的语句顺序很棘手。 documentation 声明必须在 setOutputFormat()
之前调用 setAudioSource()
。翻转语句的顺序是这样的:
Preview.getRecorderInstance().setAudioSource(MediaRecorder.AudioSource.MIC);
Preview.getRecorderInstance().setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
关注MediaRecorder文档官方即可。您的代码可能未遵循说明。函数调用顺序一定要正确
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.setOutputFile(_path);