我正在尝试在Android Studio上制作视频流应用。我想启动摄像机并将数据发送到笔记本电脑上的我自己的服务器。但是首先,我试图开始录制视频。
我从这里https://developer.android.com/guide/topics/media/camera#java开始遵循了该教程,但遇到了一些问题。
我为mediaRecorder设置了摄像机,音频和视频源,文件格式,配置文件,输出文件。然后,当我尝试设置预览显示(mediaRecorder.setPreviewDisplay( mPreview.getHolder().getSurface());
)时,出现以下错误:
“ E / MediaRecorderJNI:应用程序丢失了表面”
然后,当我尝试使用mediaRecorder.prepare()
时,它将崩溃:
“无效的预览图”
mPreview
变量被声明为mPreview = new CameraPreview(this, mCamera);
(其中CameraPreview
是上面链接中显示的类)。
您有什么建议,如何预防? 谢谢!
private boolean prepareVideoRecorder() {
mCamera = getCameraInstance(whichCamera);
mPreview = new CameraPreview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview);
mediaRecorder = new MediaRecorder();
// Step 1: Unlock and set camera to MediaRecorder
mCamera.unlock();
mediaRecorder.setCamera(mCamera);
// Step 2: Set sources
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
// Customise your profile based on a pre-existing profile
CamcorderProfile profile = CamcorderProfile.get(Camera.CameraInfo.CAMERA_FACING_FRONT,CamcorderProfile.QUALITY_LOW);
profile.fileFormat = MediaRecorder.OutputFormat.MPEG_4;
profile.videoCodec = MediaRecorder.VideoEncoder.MPEG_4_SP;
profile.videoFrameHeight = 240;
profile.videoFrameWidth = 320;
profile.videoBitRate = 15;
// Apply to MediaRecorder
mediaRecorder.setProfile(profile);
// Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
// Step 4: Set output file
mediaRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString());
// Step 5: Set the preview output
**// 1: HERE I set the previewDisplay.**
mediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());
// Step 6: Prepare configured MediaRecorder
try {
**// 2: HERE will crash with "invalid surface preview"**
mediaRecorder.prepare();
} catch (IllegalStateException e) {
Log.d(TAG, "IllegalStateException preparing MediaRecorder: " + e.getMessage());
releaseMediaRecorder();
return false;
} catch (IOException e) {
Log.d(TAG, "IOException preparing MediaRecorder: " + e.getMessage());
releaseMediaRecorder();
return false;
}
return true;
}