我正在创建一个应用程序来录制带有音频的视频并将其保存到 SD卡。它录制得非常好,但是当播放视频时,音频输出很棒,但视频拍摄效果不佳。
下面是
这是代码
public class BroadcastScreen extends Activity implements OnClickListener {
Camera camera;
FrameLayout live;
protected CameraPreview preview;
protected FileManager manager;
protected MediaRecorder mediaRecorder;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.broadcast);
live = (FrameLayout) findViewById(R.id.camera_live);
Button capture = (Button) findViewById(R.id.butPicture);
Button video = (Button) findViewById(R.id.butVideo);
capture.setOnClickListener(this);
video.setOnClickListener(this);
manager = new FileManager(this);
if (checkCameraHardware()) {
camera = getCamera();
if (camera != null) {
preview = new CameraPreview(this, camera);
live.addView(preview);
} else {
showError("Error in opening camera");
}
}
}
public static Camera getCamera() {
Camera c = null;
try {
c = Camera.open();
} catch (Exception e) {
e.printStackTrace();
}
return c;
}
private boolean setupVideoRecorder() {
// TODO Auto-generated method stub
mediaRecorder = new MediaRecorder();
try {
camera.unlock();
mediaRecorder.setCamera(camera);
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
mediaRecorder.setOutputFile(manager.setVideoFile(2).getPath());
mediaRecorder.setPreviewDisplay(preview.getHolder().getSurface());
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
mediaRecorder.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
releaseMediaRecorder();
return false;
} catch (IOException e) {
e.printStackTrace();
releaseMediaRecorder();
return false;
}
return true;
}
private PictureCallback mPicture = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
// TODO Auto-generated method stub
manager.saveAnyFile(data, 1);
camera.stopPreview();
camera.startPreview();
}
};
private boolean recording = false;
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.butPicture:
camera.takePicture(null, null, mPicture);
break;
case R.id.butVideo:
if (recording) {
mediaRecorder.stop();
releaseMediaRecorder();
camera.lock();
Button but = (Button) v;
but.setText("Record");
recording = false;
} else if (!recording) {
if (setupVideoRecorder()) {
mediaRecorder.start();
Button but = (Button) v;
but.setText("Stop");
recording = true;
}
} else {
releaseMediaRecorder();
}
break;
}
}
public boolean checkCameraHardware() {
if (this.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
return true;
} else {
return false;
}
}
public void showError(String error) {
Toast.makeText(this, error, Toast.LENGTH_LONG).show();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
// camera.startPreview();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
releaseMediaRecorder();
releaseCamera();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
// camera.release();
}
private void releaseMediaRecorder() {
if (mediaRecorder != null) {
mediaRecorder.reset();
mediaRecorder.release();
mediaRecorder = null;
camera.lock();
}
}
private void releaseCamera() {
if (camera != null) {
camera.release();
camera = null;
}
}
}
所以我想正确录制视频作为默认的相机应用程序..感谢您的帮助
更新
好吧,我在其他设备上测试了这个应用程序,我不知道为什么,但它工作完美的视频和音频都很好,但仍然不能在我的手机上工作...我想也许是因为它不适用于 Android 4.1.2 并且因为其他手机有 Android 5.1 ....请大家帮我把它搞砸了。