我正在使用自定义相机应用程序。我正在使用mediarecorder进行自定义视图。我正在使用2个相机说前置摄像头& 后置摄像头。当我单独使用前置摄像头或后置摄像头时,它工作正常。但是我使用一个按钮将前置摄像头移到后置摄像头或者反之亦然。得到以下错误。
**java.lang.RuntimeException: Fail to connect to camera service**
我瞪大了这么多的链接。解决方案,但它们对我没有帮助......我的代码是
btn_toggle.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
flipcamera();;
Log.i("clk", "sucess");
}
});
这里是flipcamera方法。
private void flipcamera() {
if(currentCameraId == Camera.CameraInfo.CAMERA_FACING_BACK){
currentCameraId = Camera.CameraInfo.CAMERA_FACING_FRONT;
}
else {
try {
currentCameraId = Camera.CameraInfo.CAMERA_FACING_BACK;
gebackCameraInstance();
} catch (Exception e) {
e.printStackTrace();
System.out.println("mmmmmmmmmmm"+e.toString());
}
}}
private Camera gebackCameraInstance() {
try {
myCamera = Camera.open();
myCamera.setDisplayOrientation(90);
}
catch (Exception e){
e.printStackTrace();
}
return myCamera;
}
private Camera getCameraInstance(){
// Camera c = null;
Log.d("No of cameras",Camera.getNumberOfCameras()+"");
for (currentCameraId = 0; currentCameraId < Camera.getNumberOfCameras(); currentCameraId++) {
CameraInfo camInfo = new CameraInfo();
Camera.getCameraInfo(currentCameraId, camInfo);
if (camInfo.facing==(Camera.CameraInfo.CAMERA_FACING_FRONT)) {
myCamera = Camera.open(currentCameraId);
myCamera.setDisplayOrientation(90);
Camera.Parameters params = myCamera.getParameters();
params.set("cam-mode", 1);
params.set("cam_mode", 1);
params.set("camera-id", 1);
myCamera.setParameters(params);
}
}
return myCamera;
/*Camera c = null;
try {
c = Camera.open();
c.setDisplayOrientation(90);
}
catch (Exception e){
e.printStackTrace();
}
return c;*/ // returns null if camera is unavailable
}
private boolean prepareMediaRecorder(){
myCamera = getCameraInstance();
mediaRecorder = new MediaRecorder();
myCamera.unlock();
mediaRecorder.setCamera(myCamera);
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_LOW));
createDir(Environment.getExternalStorageDirectory().toString(), "Scatta");
mediaRecorder.setOutputFile("/sdcard/demo/demo1" + "temp" + ".mp4");
mediaRecorder.setMaxFileSize(5000000); // Set max file size 5M
mediaRecorder.setMaxDuration(60000);
mediaRecorder.setPreviewDisplay(myCameraSurfaceView.getHolder().getSurface());
mediaRecorder.setOrientationHint(90);
try {
mediaRecorder.prepare();
} catch (IllegalStateException e) {
releaseMediaRecorder();
return false;
} catch (IOException e) {
releaseMediaRecorder();
return false;
}
return true;
}
private void createDir(String path, String dirName){
String newFolder = "/" + dirName;
File myNewFolder = new File(path + newFolder);
myNewFolder.mkdir();
mFileTemp = new File(workingPath,newFolder);
}
@Override
public void onPause() {
super.onPause();
releaseMediaRecorder(); // if you are using MediaRecorder, release it first
releaseCamera();
myCameraPreview.removeView(myCameraSurfaceView);// release the camera immediately on pause event
}
private void releaseMediaRecorder(){
if (mediaRecorder != null) {
mediaRecorder.reset(); // clear recorder configuration
mediaRecorder.release(); // release the recorder object
mediaRecorder = null;
myCamera.lock(); // lock camera for later use
}
}
private void releaseCamera(){
if (myCamera != null){
myCamera.release(); // release the camera for other applications
myCamera = null;
}
}
public class MyCameraSurfaceView extends SurfaceView implements SurfaceHolder.Callback{
private SurfaceHolder mHolder;
private Camera mCamera;
@SuppressWarnings("deprecation")
public MyCameraSurfaceView(Context context, Camera camera) {
super(context);
mCamera = camera;
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int weight,
int height) {
if (mHolder.getSurface() == null){
return;
}
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
// mCamera.unlock();
} catch (Exception e){
e.printStackTrace();
System.out.println("surface changed error"+e);
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
// mCamera.unlock();
} catch (IOException e){
e.printStackTrace();
System.out.println("IOException"+e);
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
mCamera.release();
}
}
public static Fragment newInstance(Context _context) {
Recording_Activity f = new Recording_Activity();
return f;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
final int X = (int) event.getRawX();
final int Y = (int) event.getRawY();
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams)v.getLayoutParams();
_xDelta = X- lParams.leftMargin;
_yDelta = Y- lParams.topMargin;
System.out.println("_xaDelta value"+_xDelta);
System.out.println("_yDelta value"+_yDelta);
if(!recording){
releaseCamera();
if(!prepareMediaRecorder()){
Toast.makeText(getActivity(),
"Wait camera is preparing",
Toast.LENGTH_LONG).show();
startActivity(new Intent(getActivity(),FragmentClass.class));
}
else{
try {
mediaRecorder.start();
recording = true;
btn_record_image.setBackgroundResource(R.drawable.btn_pressed_icon);
PreferenceConstants._mViewPager.setPagingEnabled(false);
onAnimate(v);
countDownTimer.start();
iv_progress.setDrawingCacheEnabled(true);
timer.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
}
catch (Exception e) {
e.printStackTrace();
System.out.println("camera error"+e.toString());
}
}
}
break;
case MotionEvent.ACTION_UP:
File mFile = new File("The output file's absolutePath");
if(recording){
try{
mediaRecorder.stop();
PreferenceConstants._mViewPager.setPagingEnabled(true);
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) v.getLayoutParams();
layoutParams.leftMargin = X - _xDelta;
layoutParams.topMargin = Y - _yDelta;
layoutParams.rightMargin = -250;
layoutParams.bottomMargin = -250;
btn_record_image.setLayoutParams(layoutParams);
Intent i = new Intent(getActivity(),PlaybackScreen.class);
startActivity(i);
iv_progress.setDrawingCacheEnabled(false);
}
catch(RuntimeException e) {
mFile.delete(); //you must delete the outputfile when the recorder stop failed.
} finally {
releaseMediaRecorder();
mediaRecorder = null;
}
btn_record_image.setBackgroundResource(R.drawable.btn_unpressed_icon);
recording = false;
temp++;
countDownTimer.cancel();
animation.cancel();
}
break;
case MotionEvent.ACTION_POINTER_DOWN:
break;
case MotionEvent.ACTION_POINTER_UP:
break;
case MotionEvent.ACTION_MOVE:
if(check_value > 0)
{
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) v.getLayoutParams();
layoutParams.leftMargin = X - _xDelta;
layoutParams.topMargin = Y - _yDelta;
layoutParams.rightMargin = -250;
layoutParams.bottomMargin = -250;
v.setLayoutParams(layoutParams);
timer.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
}
break;
}
layout.invalidate();
return true;
}
@Override
public void onResume() {
super.onResume();
Boolean isLoggedin = sharedPreferences.getBoolean(PreferenceConstants.IS_LOGGED_IN, false);
if(isLoggedin){
PreferenceConstants._mViewPager.setPagingEnabled(true);
}
else{
PreferenceConstants._mViewPager.setPagingEnabled(false);
iv_cross.setVisibility(View.VISIBLE);
}
}
public void onAnimate(View v) {
animation = new TranslateAnimation(0.0f, 400.0f,
0.0f, 0.0f);
animation.setDuration(8000); // animation duration
iv_progress.startAnimation(animation);
animation.setFillAfter(false);
}
public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long startTime, long interval) {
super(startTime, interval);
}
@Override
public void onTick(long millisUntilFinished) {
timer.setText("" + millisUntilFinished / 1000);
timer.setGravity(Gravity.CENTER );
}
@Override
public void onFinish() {
Intent i = new Intent(getActivity(),PlaybackScreen.class);
startActivity(i);
}
}
}
请帮助我解决这个问题。我真的遇到了这个错误。
提前致谢.. :))