无效的预览表面android视频录制

时间:2012-08-26 15:49:47

标签: android android-camera

我有服务,我尝试将服务记录视频存档。

启动服务的活动:

public static SurfaceView mSurfaceView;
public static SurfaceHolder mSurfaceHolder;
public static Camera mCamera;
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mSurfaceView = (SurfaceView) findViewById(R.id.surfaceView1);
    mSurfaceHolder = mSurfaceView.getHolder();
    mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    mSurfaceHolder.addCallback(this);
    Intent intent = new Intent(MainActivity.this, RecordService.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startService(intent);
    finish();
}

服务:

private MediaRecorder mMediaRecorder;
private SurfaceView mSurfaceView;
private SurfaceHolder mSurfaceHolder;
private boolean mRecordingStatus;
private Camera mServiceCamera;
private Size  mPreviewSize;

@Override
public void onCreate() 
{
    mRecordingStatus = false;
    mSurfaceView = MainActivity.mSurfaceView;
    mSurfaceHolder = MainActivity.mSurfaceHolder;
    mServiceCamera=MainActivity.mCamera;
    InitCamera();
    super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) 
{
    super.onStartCommand(intent, flags, startId);
    if (!mRecordingStatus)
        StartRecord();
    else
        StopRecord();

    return START_STICKY;
}
private void InitCamera()
{
    mServiceCamera = Camera.open();
    Camera.Parameters p = mServiceCamera.getParameters();

    final List<Size> listSize = p.getSupportedPreviewSizes();
    mPreviewSize = listSize.get(2);
    p.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
    p.setPreviewFormat(PixelFormat.YCbCr_420_SP);
    mServiceCamera.setParameters(p);
    try 
    {
        mServiceCamera.setPreviewDisplay(mSurfaceHolder);
        mServiceCamera.startPreview();
    }
    catch (IOException e) 
    {

        e.printStackTrace();
    }

    mServiceCamera.unlock();
}
private void StartRecord()
{
    try
    {
        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(Environment.getExternalStorageDirectory().getPath()+File.separator+ "video.mp4");
        mMediaRecorder.setVideoFrameRate(30);
        mMediaRecorder.setVideoSize(mPreviewSize.width, mPreviewSize.height);
        mMediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());

        mMediaRecorder.prepare();
        mMediaRecorder.start(); 

        mRecordingStatus = true;

        Toast toast = Toast.makeText(getBaseContext(), "Recording",1000);
        toast.show();
    }
    catch (IOException e) 
    {
        Toast toast = Toast.makeText(getBaseContext(), e.getMessage(),2000);
        toast.show();
    }
}

当我调试我的Galaxy上的代码时,我得到了吐司(在StartRecord()方法中)&#34;无效的预览表面&#34;

如何解决?

2 个答案:

答案 0 :(得分:16)

您看到的原因是因为您在创建曲面之前执行了mMediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());。添加延迟恰好可行,但不是处理此问题的正确方法。

mSurfaceHolder = mSurfaceView.getHolder();行不会立即创建曲面。实际上,SurfaceHolder提供了一个回调接口,可以让您知道何时创建了曲面。

mSurfaceHolder.addCallback(this);应该在您创建的类的构造函数中,该类扩展了SurfaceView并实现了SurfaceHolder.Callback。然后,您可以覆盖SurfaceHolder.Callback接口的回调方法,以便在创建表面后执行代码。这是一个例子:

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {

private SurfaceHolder surfaceHolder;


public CameraPreview(Context context, VideoHandler vh) {
    super(context);
    videoHandler = vh;

    // Install a SurfaceHolder.Callback so we get notified when the
    // underlying surface is created and destroyed.
    surfaceHolder = getHolder();
    surfaceHolder.addCallback(this);
    // deprecated setting, but required on Android versions prior to 3.0
    if(Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB)
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    // Perform actions that require Surface to have been created here e.g.
    // mMediaRecorder.setPreviewDisplay(holder.getSurface()); etc.

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    // Take care of releasing camera preview etc.
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    // Handle changes
}

}

答案 1 :(得分:0)

我不得不延迟执行MediaRecorder启动。这意味着在onResume中,我创建了一个Handler并在其上调用了postDelayed。我知道它仍然是相同的线程,但它与我合作。