我正在使用表面视图制作自定义相机,但我在录制视频时遇到了一些奇怪的问题,
java.lang.NullPointerException
at com.dts.stallchat.activities.ImageCameraActivity$9.onClick(ImageCameraActivity.java:304)
at android.view.View.performClick(View.java:4222)
at android.view.View$PerformClick.run(View.java:17620)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5391)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
实施代码
//Release Camera before MediaRecorder start
releaseCamera();
if(!prepareMediaRecorder()){
Toast.makeText(_thisActivity,
"Fail in prepareMediaRecorder()!\n - Ended -",
Toast.LENGTH_LONG).show();
//finish();
}
/*if(mediaRecorder==null){
mediaRecorder= new MediaRecorder();
}*/
recordingTime.setBase(SystemClock.elapsedRealtime());
recordingTime.start();
recordingTime.setVisibility(View.VISIBLE);
mediaRecorder.start(); // this is line 304
is_recording = true;`
方法:
private void releaseMediaRecorder(){
if (mediaRecorder != null) {
mediaRecorder.reset(); // clear recorder configuration
mediaRecorder.release(); // release the recorder object
mediaRecorder = null;
camera.lock(); // lock camera for later use
}
}
private void releaseCamera(){
if (camera != null){
camera.release(); // release the camera for other applications
camera = null;
}
}
private boolean prepareMediaRecorder(){
try{
camera = getCameraInstance();
Parameters parameters = camera.getParameters();
int displayOrt=setGetCameraDisplayOrientation(_thisActivity, currentCameraId, camera);
if(!parameters.getFlashMode().equalsIgnoreCase(Parameters.FLASH_MODE_OFF)){
parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
}else {
parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
}
Camera.Size prevSize=StallchatCameraUtils.getBestAspectPreviewSize(displayOrt, wid, wid, parameters);
// Camera.Size prevSize=getBestPreviewSize(wid, wid, parameters);
parameters.setPreviewSize(prevSize.width, prevSize.height);
camera.setParameters(parameters);
mediaRecorder = new MediaRecorder();
camera.unlock();
mediaRecorder.setCamera(camera);
// mediaRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
//mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
//mediaRecorder.setVideoSize(wid, wid);
//mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
mediaRecorder.setOutputFile(getVideoPath().getAbsolutePath());
mediaRecorder.setMaxDuration(60000); // Set max duration 60 sec.
mediaRecorder.setMaxFileSize(20000000); // Set max file size 20M
//cameraSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mediaRecorder.setPreviewDisplay(cameraSurfaceHolder.getSurface());
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return false;
}
try {
mediaRecorder.prepare();
} catch (IllegalStateException e) {
releaseMediaRecorder();
return false;
} catch (IOException e) {
releaseMediaRecorder();
return false;
}
return true;
}
我在以下设备上遇到此问题 -
Samsung Note 2(android 4.4.2)
Micromax A114(android 4.2.2)
但在其他设备中,如motorola moto g(4.4.4)
或三星galaxy s2工作得很好
答案 0 :(得分:0)
将android:hardwareAccelerated =“false”粘贴到您的清单文件中。