我正在使用服务拍摄背景照片,然后切换到录制视频。切换到mediarecorder时,我总是收到错误:
08-10 09:09:06.999 15263-15263/quantum_soft.com.vidsense E/MediaRecorderJNI: Application lost the surface
08-10 09:09:06.999 15263-15263/quantum_soft.com.vidsense D/RecorderService: invalid preview surface
08-10 09:09:06.999 15263-15263/quantum_soft.com.vidsense W/System.err: java.io.IOException: invalid preview surface
08-10 09:09:06.999 15263-15263/quantum_soft.com.vidsense W/System.err: at android.media.MediaRecorder._prepare(Native Method)
08-10 09:09:06.999 15263-15263/quantum_soft.com.vidsense W/System.err: at android.media.MediaRecorder.prepare(MediaRecorder.java:582)
08-10 09:09:07.009 15263-15263/quantum_soft.com.vidsense W/System.err: at quantum_soft.com.vidsense.RecorderService.startRecording(RecorderService.java:78)
08-10 09:09:07.009 15263-15263/quantum_soft.com.vidsense W/System.err: at quantum_soft.com.vidsense.RecorderService$1.onPictureTaken(RecorderService.java:39)
该服务能够拍摄照片或录制视频,但我不能让它一个接一个地做,而不会丢失表面。我已经尝试了各种组合释放相机,预览等但到目前为止没有运气。
我的服务是:
public class RecorderService extends Service {
private static final String TAG = "RecorderService";
private SurfaceView mSurfaceView;
private SurfaceHolder mSurfaceHolder;
private static Camera mServiceCamera;
private MediaRecorder mMediaRecorder;
public static final int MEDIA_TYPE_IMAGE = 1;
public static final int MEDIA_TYPE_VIDEO = 2;
Camera.PictureCallback jpegCallback;
@Override
public void onCreate() {
mSurfaceView = MainActivity.mSurfaceView;
mSurfaceHolder = MainActivity.mSurfaceHolder;
jpegCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
startRecording();
}
};
takePhoto();
}
public void takePhoto(){
mServiceCamera = getCameraInstance();
try {
mServiceCamera.setPreviewDisplay(mSurfaceHolder);
mServiceCamera.startPreview();
}
catch (IOException e) {
Log.e(TAG, e.getMessage());
e.printStackTrace();
}
mServiceCamera.takePicture(null, null, jpegCallback);
}
public boolean startRecording(){
showLog("Recording Started");
releaseCamera();
mServiceCamera=getCameraInstance();
try {
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);
mMediaRecorder.setOutputFile("/sdcard/video.mp4");
mMediaRecorder.setVideoFrameRate(30);
mMediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());
mMediaRecorder.prepare();
mMediaRecorder.start();
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;
}
}
private Camera getCameraInstance(){
Camera c = null;
try {
c = Camera.open();
}
catch (Exception e){
}
return c;
}
private void releaseCamera(){
if (mServiceCamera != null){
mServiceCamera.stopPreview();
mServiceCamera.setPreviewCallback(null);
mServiceCamera.release();
mServiceCamera = null;
}
}
public void showMsg(String msg) {
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
}
public void showLog(String msg) {
Log.d("VidTest", msg);
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
我的主要是:
public class MainActivity extends Activity {
private static final String TAG = "Recorder";
public static SurfaceView mSurfaceView;
public static SurfaceHolder mSurfaceHolder;
public static SurfaceView mSurfaceView2;
public static SurfaceHolder mSurfaceHolder2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
File mediaStorageDir = new File("/sdcard", "VidTest");
mediaStorageDir = new File(Environment.getExternalStorageDirectory().getPath() + "/VidTest/", "vids");
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()) {
showMsg("Failed to create directories");
finish();
}
}
mSurfaceView = (SurfaceView) findViewById(R.id.surfaceView1);
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
Button btnStart = (Button) findViewById(R.id.StartService);
btnStart.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showMsg("Moitoring service started");
Intent intent = new Intent(MainActivity.this, RecorderService.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startService(intent);
finish();
}
});
Button btnStop = (Button) findViewById(R.id.StopService);
btnStop.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
stopService(new Intent(MainActivity.this, RecorderService.class));
}
});
}
public void showMsg(String msg) {
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
}
public void showLog(String msg) {
Log.d("VidSense", msg);
}
}