在后台录制视频 - Android

时间:2016-07-07 09:31:43

标签: android service video-recording

通过stackoverflow或其他地方的各种来源,我收集了以下代码,以便在后台录制视频,但我无法启动我的RecorderService。

ACTIVITY -1 变量:

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

致电Recorder服务:

@Override
public void surfaceCreated(SurfaceHolder holder) {
    Log.e("Suface created - intent", "recorder service");
    Intent intent2 = new Intent(AttendantActivity.this, RecorderService.class);
    startService(intent2);
}

RecorderService - 来源:https://github.com/pickerweng/CameraRecorder

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

    @Override
    public void onCreate() {
        mRecordingStatus = false;
        mServiceCamera = AttendantActivity.mCamera;
        mSurfaceView = AttendantActivity.mSurfaceView;
        mSurfaceHolder = AttendantActivity.mSurfaceHolder;
        Log.e("Recorder", "Service started");
        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() {
        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();
        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() +"/"+ new Date(System.currentTimeMillis()).toString()+"video.mp4");
        mMediaRecorder.setVideoSize(320, 240);
        if(mSurfaceHolder != null)
            mMediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());
        try {
            mMediaRecorder.prepare();
            mMediaRecorder.start();    
        } catch (IOException e) {
            e.printStackTrace();
        }
        mRecordingStatus = true;
        return true;    
    }

    public void stopRecording() {
        Toast.makeText(getBaseContext(), "Recording Stopped", Toast.LENGTH_SHORT).show();
        try {
            mServiceCamera.reconnect();    
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            mMediaRecorder.stop();
            Log.e("[AUDIO---]", "recording stopped-");
        }catch (RuntimeException e){
            Log.e("Exception", e.getMessage()+"-");
        }
        if(mMediaRecorder!=null) {
            mMediaRecorder.reset();   // You can reuse the object by going back to setAudioSource() step
            mServiceCamera.stopPreview();
            mMediaRecorder.release();
        }

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

0 个答案:

没有答案