是否可以在服务中录制视频,即不设置setPreviewDisplay? 我在HTC Desire上尝试过它,但是它会在日志中抛出这些信息,
MediaRecorder准备失败:-1
CameraInput没有表面可供显示
是否需要设置一些其他属性?
答案 0 :(得分:1)
我在某个地方发现了这个片段。不确定它是否适用于媒体录像机,但它对于HTC英雄android 2.1-update1(和模拟器)上的camera.takePicture工作得相当好。
if (camera == null) {
Log.i(TAG, "Opening camera");
camera = Camera.open();
}
SurfaceView view = new SurfaceView(new DummyContext()); //You'll have to create your own class extending Context
camera.setPreviewDisplay(view.getHolder());
camera.startPreview();
//wait
Log.i(TAG, "Wait for cam");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
Log.e(TAG, "Woken violently");
e.printStackTrace();
}
//take pic
Log.i(TAG, "Take pic");
camera.takePicture(shutterCallback, rawCallback, jpegCallback);
答案 1 :(得分:0)
我相信根据文档
public final void setPreviewDisplay (SurfaceHolder持有人)
自:API Level 1将Surface设置为 用于实时预览。 表面是 预览所需,预览是 需要拍照。相同 表面可以重新设置而不会造成伤害。
我知道您希望拍摄视频但是,我认为任何相机的使用需要一个表面。
答案 2 :(得分:0)
public class RecorderService extends Service { private static final String TAG =“RecorderService”; 私有SurfaceView mSurfaceView; 私人SurfaceHolder mSurfaceHolder; private static Camera mServiceCamera; private boolean mRecordingStatus; 私人MediaRecorder mMediaRecorder; private File filefolder; private String strphoto; 私人文件照片;
@Override
public void onCreate() {
mRecordingStatus = false;
//mServiceCamera = CameraRecorder.mCamera;
mServiceCamera = Camera.open();
mSurfaceView = CameraRecorder.mSurfaceView;
mSurfaceHolder = CameraRecorder.mSurfaceHolder;
super.onCreate();
if (mRecordingStatus == false)
startRecording();
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onDestroy() {
stopRecording();
mRecordingStatus = false;
super.onDestroy();
}
public boolean startRecording(){
try {
Toast.makeText(getBaseContext(), "Recording Started", Toast.LENGTH_SHORT).show();
//mServiceCamera = Camera.open();
Camera.Parameters params = mServiceCamera.getParameters();
mServiceCamera.setParameters(params);
Camera.Parameters p = mServiceCamera.getParameters();
final List<Size> listSize = p.getSupportedPreviewSizes();
Size mPreviewSize = listSize.get(2);
Log.v(TAG, "use: width = " + mPreviewSize.width
+ " height = " + mPreviewSize.height);
p.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
p.setPreviewFormat(PixelFormat.YCbCr_420_SP);
mServiceCamera.setParameters(p);
try {
mServiceCamera.setPreviewDisplay(mSurfaceHolder);
mServiceCamera.startPreview();
}
catch (IOException e) {
Log.e(TAG, e.getMessage());
e.printStackTrace();
}
mServiceCamera.unlock();
mMediaRecorder = new MediaRecorder();
mMediaRecorder.setCamera(mServiceCamera);
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
{
filefolder=new File(android.os.Environment.getExternalStorageDirectory(),"Backgroundcamera/videos");
}
else
{
filefolder=RecorderService.this.getCacheDir();
}
if(!filefolder.exists())
filefolder.mkdirs();
strphoto = System.currentTimeMillis()+".mp4";
mMediaRecorder.setOutputFile(filefolder+"/"+strphoto);
mMediaRecorder.setVideoFrameRate(30);
mMediaRecorder.setVideoSize(mPreviewSize.width, mPreviewSize.height);
mMediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());
mMediaRecorder.prepare();
mMediaRecorder.start();
mRecordingStatus = true;
return true;
} catch (IllegalStateException e) {
Log.d(TAG, e.getMessage());
e.printStackTrace();
return false;
} catch (IOException e) {
Log.d(TAG, e.getMessage());
e.printStackTrace();
return false;
}
}
public void stopRecording() {
Toast.makeText(getBaseContext(), "Recording Stopped", Toast.LENGTH_SHORT).show();
try {
mServiceCamera.reconnect();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mMediaRecorder.stop();
mMediaRecorder.reset();
mServiceCamera.stopPreview();
mMediaRecorder.release();
mServiceCamera.release();
mServiceCamera = null;
}
} 和
点击listerner启动服务。