我正在使用前置摄像头的代码。它在android 4.0.4中运行良好。但不适用于Android 4.1.2。启动媒体记录器时出错。如何解决。
我的课程:
public class RecoredVideo extends Activity implements OnClickListener,
OnInfoListener {
private Camera myCamera;
private MyCameraSurfaceView myCameraSurfaceView;
private MediaRecorder mediaRecorder;
File file;
FrameLayout myCameraPreview;
String Webpath;
String localSdpath;
Button recordstart, close,cameraChange;
boolean recording;
ProgressBar progressBar;
String VideoPath;
CountDownTimer mCountDown;
int myProgress = 0;
Camera camera = null;
int currentCameraId;
int Rotate=270;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera);
RecorderPrepare();
recordstart = (Button) findViewById(R.id.videostart);
recordstart.setOnClickListener(this);
recording = false;
close = (Button) findViewById(R.id.CloseBtn);
close.setOnClickListener(this);
progressBar = (ProgressBar) findViewById(R.id.recordbar);
progressBar.setVisibility(View.GONE);
cameraChange=(Button) findViewById(R.id.CamerachangeBtn);
cameraChange.setOnClickListener(this);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
@SuppressLint("NewApi")
public int getFrontCameraId() {
CameraInfo ci = new CameraInfo();
for (int i = 0 ; i < Camera.getNumberOfCameras(); i++) {
Camera.getCameraInfo(i, ci);
if (ci.facing == CameraInfo.CAMERA_FACING_FRONT) return i;
}
return -1; // No front-facing camera found
}
@SuppressLint("NewApi")
private Camera getCameraInstance() {
// TODO Auto-generated method stub
try {
int index = getFrontCameraId();
if(index == -1){
currentCameraId=Camera.CameraInfo.CAMERA_FACING_BACK;
}else{
currentCameraId=Camera.CameraInfo.CAMERA_FACING_FRONT;
}
camera = Camera.open(currentCameraId);
camera.setDisplayOrientation(90);
// camera = Camera.open();
Camera.Parameters param = camera.getParameters();
param.set("cam_mode", 1);
// startCamera(Camera.Parameters.CAMERA_FACING_BACK);
// param.setFocusMode(Camera.Parameters.FOCUS_MODE_INFINITY);
param.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
camera.setParameters(param);// attempt to get a Camera instance
} catch (Exception e) {
// Camera is not available (in use or does not exist)
}
return camera; // returns null if camera is unavailable
}
@SuppressLint("NewApi")
private void cameraChange() {
camera.stopPreview();
camera.release();
if(currentCameraId == Camera.CameraInfo.CAMERA_FACING_BACK){
currentCameraId = Camera.CameraInfo.CAMERA_FACING_FRONT;
Rotate=270;
}
else {
currentCameraId = Camera.CameraInfo.CAMERA_FACING_BACK;
Rotate=90;
}
camera = Camera.open(currentCameraId);
camera.setDisplayOrientation(90);
myCameraSurfaceView = new MyCameraSurfaceView(this, camera);
myCameraSurfaceView.requestFocus();
myCameraPreview = (FrameLayout) findViewById(R.id.firstrecordview);
int count=myCameraPreview.getChildCount();
if(count!=0){
myCameraPreview.removeAllViews();
}
if (myCameraPreview != null && myCameraSurfaceView != null) {
myCameraPreview.addView(myCameraSurfaceView);
}
}
@SuppressLint("NewApi")
private boolean prepareMediaRecorder() {
myCamera = getCameraInstance();
// myCamera.setDisplayOrientation(90);
mediaRecorder = new MediaRecorder();
myCamera.unlock();
mediaRecorder.setCamera(myCamera);
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setProfile(CamcorderProfile
.get(CamcorderProfile.QUALITY_HIGH));
VideoPath=initFile().getAbsolutePath();
mediaRecorder.setOutputFile(VideoPath);
mediaRecorder.setMaxDuration(60000); // Set max duration 60 sec.
mediaRecorder.setOnInfoListener(this);
mediaRecorder.setMaxFileSize(5242880 * 4);// Set max file size 5M
//if(currentCameraId == Camera.CameraInfo.CAMERA_FACING_BACK){
//mediaRecorder.setOrientationHint(90);
//}
//else {
mediaRecorder.setOrientationHint(Rotate);
//}
mediaRecorder.setPreviewDisplay(myCameraSurfaceView.getHolder()
.getSurface());
try {
mediaRecorder.prepare();
} catch (IllegalStateException e) {
releaseMediaRecorder();
return false;
} catch (IOException e) {
releaseMediaRecorder();
return false;
}
return true;
}
public void onInfo(MediaRecorder mr, int what, int extra) {
Log.i("onInfo:", what + ":::"
+ MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED );
if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED ) {
Log.i("VIDEOCAPTURE", "Maximum Duration Reached");
Stop();
}
}
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;
}
}
private File initFile() {
File dir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES),
this.getClass().getPackage().getName());
if (!dir.exists() && !dir.mkdirs()) {
this.file = null;
} else {
this.file = new File(dir.getAbsolutePath(), new SimpleDateFormat(
"'MOV_'yyyyMMddHHmmss'.mp4'").format(new Date()));
System.out.println(this.file + "::::::this.file");
}
return this.file;
}
private void RecorderPrepare() {
myCamera = getCameraInstance();
if (myCamera == null) {
}
myCameraSurfaceView = new MyCameraSurfaceView(this, myCamera);
myCameraSurfaceView.requestFocus();
myCameraPreview = (FrameLayout) findViewById(R.id.firstrecordview);
if (myCameraSurfaceView != null) {
}
if (myCameraPreview != null && myCameraSurfaceView != null) {
myCameraPreview.addView(myCameraSurfaceView);
}
}
private void RecordStart() {
if (recording) {
recordstart
.setBackgroundResource(R.drawable.record_button_inactive_2);
// stop recording and release camera
Stop();
} else {
// Release Camera before MediaRecorder start
releaseCamera();
if (!prepareMediaRecorder()) {
finish();
}
recordstart.setBackgroundResource(R.drawable.record_button_active_2);
mediaRecorder.start();
recording = true;
}
}
private void Stop() {
if (mediaRecorder != null)
mediaRecorder.stop();
progressBar.setVisibility(View.GONE);
if (mCountDown != null) {
mCountDown.cancel();
}
releaseMediaRecorder(); // release the MediaRecorder object
releaseCamera();
Intent Intent_imageview = new Intent(RecoredVideo.this,
ViewAndSend.class);
Intent_imageview.putExtra("path", VideoPath);
startActivity(Intent_imageview);
finish();
}
@Override
protected void onPause() {
super.onPause();
System.out.println("onPause::::00" + ":::::");
if (mCountDown != null) {
mCountDown.cancel();
}
try {
releaseMediaRecorder(); // if you are using MediaRecorder, release
releaseCamera(); // release the camera immediately on pause event
} catch (Exception e) {
Log.e("onPause", e + "");
}
finish();
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
System.out.println("onPause::::00" + ":::::");
if (mCountDown != null) {
mCountDown.cancel();
}
try {
releaseMediaRecorder(); // if you are using MediaRecorder, release
// it
releaseCamera(); // release the camera immediately on pause event
} catch (Exception e) {
Log.e("onPause", e + "");
}
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.videostart) {
cameraChange.setVisibility(View.GONE);
RecordStart();
startTimer1();
} else if (v.getId() == R.id.CloseBtn) {
progressBar.setVisibility(View.GONE);
if (mediaRecorder != null)
mediaRecorder.stop();
if (mCountDown != null) {
mCountDown.cancel();
}
releaseMediaRecorder(); // release the MediaRecorder object
releaseCamera();
finish();
}else if(v.getId() == R.id.CamerachangeBtn){
cameraChange();
}
}
private void startTimer1() {
progressBar.setVisibility(View.VISIBLE);
myProgress=0;
progressBar.setSecondaryProgress(0);
progressBar.setMax(60);
// Log.i("bulb", "msg");
if (mCountDown != null) {
mCountDown.cancel();
}
mCountDown = new CountDownTimer(60000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
myProgress++;
progressBar.setProgress(myProgress);
}
@Override
public void onFinish() {
// Log.i("bulb", "msg");
progressBar.setVisibility(View.GONE);
}
}.start();
}
}
错误:
java.lang.RuntimeException: start failed.
at android.media.MediaRecorder.start(Native Method)
at com.gigglemail.giggle.RecoredVideo.RecordStart(RecoredVideo.java:272)
at com.gigglemail.giggle.RecoredVideo.onClick(RecoredVideo.java:340)
at android.view.View.performClick(View.java:4223)
at android.view.View$PerformClick.run(View.java:17275)
在showast xml中添加了所有权限