顺便说一句,别介意评论。
我深入研究了文档和您的代码并得到了以下结果。
这是调用 mediaRecorder 方法来获取表面的顺序。
mMediaRecorder.prepare();
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mMediaRecorder.setVideoEncodingBitRate(512 * 1000);
mMediaRecorder.setVideoFrameRate(30);
mMediaRecorder.setVideoSize(width, height);
mMediaRecorder.setOutputFile(filePath);
这就是文档 http://developer.android.com/reference/android/media/MediaRecorder.html#setVideoEncoder%28int%29 says
//Call this method before prepare().
setVideoEncodingBitRate(); //no exception thrown
//Must be called after setVideoSource(). Call this after setOutFormat() but before prepare().
setVideoSize(width, height); //IllegalStateException if it is called after prepare() or before setOutputFormat()
//Call this only before setOutputFormat().
setAudioSource(); //IllegalStateException if it is called after setOutputFormat()
setVideoSource(); //IllegalStateException if it is called after setOutputFormat()
//Call this after setOutputFormat() and before prepare().
setVideoEncoder(); //IllegalStateException if it is called before setOutputFormat() or after prepare()
setAudioEncoder(); //IllegalStateException if it is called before setOutputFormat() or after prepare().
//Call this after setAudioSource()/setVideoSource() but before prepare().
setOutputFormat(); //IllegalStateException if it is called after prepare() or before setAudioSource()/setVideoSource().
//Call this after setOutputFormat() but before prepare().
setOutputFile(); //IllegalStateException if it is called before setOutputFormat() or after prepare()
//Must be called after setVideoSource(). Call this after setOutFormat() but before prepare().
setVideoFrameRate(); //IllegalStateException if it is called after prepare() or before setOutputFormat().
//This method must be called after setting up the desired audio and video sources, encoders, file format, etc., but before start()
prepare() //IllegalStateException if it is called after start() or before setOutputFormat().
因此,为了使 mediaRecorder 处于正确的状态,您必须按以下顺序调用方法:
setAudioSource()
setVideoSource()
setOutputFormat()
setAudioEncoder()
setVideoEncoder()
setVideoSize()
setVideoFrameRate()
setOutputFile()
setVideoEncodingBitRate()
prepare()
start()
我想当我调用集合时我也遇到了一个未记录的错误设置之前的编码器方法来源方法
编辑:我以为我得到了工作代码,但我仍然得到 IllegalStateExceptions,尽管代码是按照文档的顺序排列的。
Edit2:我现在可以使用了。可能也不起作用的事情和其他错误消息:
- 未设置外部存储和麦克风的权限(在清单中添加使用权限)
- Android MediaRecorder 在无效状态 4 下启动失败 https://stackoverflow.com/questions/19558812/android-mediarecorder-start-failed-in-invalid-state-4
- 重新启动 Android MediaRecorder 时出现 IllegalStateException [在无效状态下调用启动:1] https://stackoverflow.com/questions/21396140/illegalstateexception-start-called-in-an-invalid-state-1-on-restarting-androi
我必须创建一个应用程序可以写入的目录。我无法让外部存储工作,所以我使用了
数据目录。但这与mMediaRecorder
code
这段代码的工作原理:
private void prepareRecording() {
//Deal with FileDescriptor and Directory here
//Took audio out because emulator has no mic
//mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecorder.setVideoEncodingBitRate(512 * 1000);
//mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mMediaRecorder.setVideoSize(width, height);
mMediaRecorder.setVideoFrameRate(30);
mMediaRecorder.setOutputFile(filePath);
try {
mMediaRecorder.prepare();
} catch (Exception e) {
e.printStackTrace();
return;
}
//Field variable to hold surface object
//Deal with it as you see fit
surface = mMediaRecorder.getSurface();
Beware虽然上面的代码可以创建MediaRecorder
正确地写入存储,它会导致整个模拟器崩溃mMediaRecorder.stop()
叫做。