解锁后,相机屏幕变黑。通过服务实现摄像机记录

时间:2019-03-11 13:58:06

标签: android service android-camera surfaceview surfaceholder

我正在创建一个即使在屏幕锁定时也可以录制视频的应用程序。视频正在录制,但解锁手机后,相机预览会变黑。 尝试了各种stackoverflow答案以覆盖onPause和onResume方法,但对我没有任何帮助。因此,如果有人可以提供帮助,那将是一个很大的安慰。

CameraRecorder活动

public class CameraRecorder extends Activity implements SurfaceHolder.Callback {

private static final String TAG = CameraRecorder.class.getSimpleName();

public static SurfaceView mSurfaceView;
public static SurfaceHolder mSurfaceHolder;
public static Camera mCamera;
public static boolean mPreviewRunning;



/**
 * Called when the activity is first created.
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mSurfaceView = (SurfaceView) findViewById(R.id.surfaceView1);
    mSurfaceHolder = mSurfaceView.getHolder();
    mSurfaceHolder.addCallback(this);
    mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    Button btnStart = (Button) findViewById(R.id.StartService);
    btnStart.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent(CameraRecorder.this, RecorderService.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startService(intent);

        }
    });

    Button btnStop = (Button) findViewById(R.id.StopService);
    btnStop.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            stopService(new Intent(CameraRecorder.this, RecorderService.class));
        }
    });
}
@Override
public void onPause() {
    try {
        mSurfaceView.setVisibility(View.GONE);
        mSurfaceHolder.removeCallback(this);
        mCamera.release();
    } catch (Exception e) {
        e.printStackTrace();
    }
    super.onPause();
}

@Override
protected void onResume() {
    super.onResume();


        try {
            mCamera = Camera.open();
            mSurfaceHolder.addCallback(this);
            mSurfaceView.setVisibility(View.VISIBLE);
        } catch (Exception e) {
            e.printStackTrace();
        }

}

@Override
public void surfaceCreated(SurfaceHolder holder) {

}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}}

RecorderService.java

public class RecorderService extends Service {
private static final String TAG = "RecorderService";
private SurfaceView mSurfaceView;
public static SurfaceHolder mSurfaceHolder;
public static Camera mServiceCamera;
public static boolean mRecordingStatus;
public static MediaRecorder mMediaRecorder;


@Override
public void onCreate() {
    Log.e("countcreate","working");
    mRecordingStatus = false;
    mServiceCamera = CameraRecorder.mCamera;
    mSurfaceView = CameraRecorder.mSurfaceView;
    mSurfaceHolder = CameraRecorder.mSurfaceHolder;

    super.onCreate();
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);

    if (mRecordingStatus == false)
        startRecording();

    return START_STICKY;
}

@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> listPreviewSize = p.getSupportedPreviewSizes();
        for (Size size : listPreviewSize) {
            Log.i(TAG, String.format("Supported Preview Size (%d, %d)", size.width, size.height));
        }

        Size previewSize = listPreviewSize.get(0);
        p.setPreviewSize(previewSize.width, previewSize.height);
        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.THREE_GPP);
        mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
        mMediaRecorder.setOutputFile(Environment.getExternalStorageDirectory().getPath() + "/video.mp4");
        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) {
        e.printStackTrace();
    }

    mMediaRecorder.stop();
    mMediaRecorder.reset();

    mServiceCamera.stopPreview();
    mMediaRecorder.release();

    mServiceCamera.release();
    mServiceCamera = null;
}}

0 个答案:

没有答案