it-swarm.dev

如何从应用程序的背景录制视频:Android

我正在开发一个应用程序,它将能够使用Service从应用程序的后台录制视频。

问题描述:

在我的应用程序录音将被安排。如果用户想要将视频从1 PM录制到下午3点,他将安排任务并退出应用程序。申请将在下午1点至3点自动开始录制。

我做了什么:

我搜索了我的查询,但没有得到解决方案。许多文章说这是不可能的。但在 Google Play 有一些应用程序(例如 MyCar Recorder )可以从应用程序的后台录制视频。

我得到了 文章 关于相同但不起作用。

实现此功能的方法是什么?

58
Android Learner

1-我创建了一个活动来启动这样的服务:

package com.Android.camerarecorder;

import Android.app.Activity;
import Android.content.Intent;
import Android.hardware.Camera;
import Android.os.Bundle;
import Android.view.SurfaceHolder;
import Android.view.SurfaceView;
import Android.view.View;
import Android.widget.Button;

public class CameraRecorder extends Activity implements SurfaceHolder.Callback {
    private static final String TAG = "Recorder";
    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);
                finish();
            }
        });

        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 surfaceCreated(SurfaceHolder holder) {

    }

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

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub

    }
}

2 - 现在我创建了 在后台录制视频的服务 像这样:

    package com.Android.camerarecorder;
    import Java.io.IOException;
    import Java.util.List;
    import Android.app.Service;
    import Android.content.Intent;
    import Android.graphics.PixelFormat;
    import Android.hardware.Camera;
    import Android.hardware.Camera.Size;
    import Android.media.MediaRecorder;
    import Android.os.IBinder;
    import Android.util.Log;
    import Android.view.SurfaceHolder;
    import Android.view.SurfaceView;
    import Android.widget.Toast;

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

        @Override
        public void onCreate() {
            mRecordingStatus = false;
            //mServiceCamera = CameraRecorder.mCamera;
            mServiceCamera = Camera.open(1);
            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);
                mMediaRecorder.setOutputFile("/sdcard/video.mp4");
                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;
        }
    }

它会在你的SD卡中创建一个文件video.mp4。您可以更改代码以添加更多功能,但基本功能是通过此代码实现的,即在后台录制视频。

注意:我已通过按钮点击活动开始服务,但您可以通过任何其他方式启动它,如广播接收器等。

希望能帮助到你!!干杯。

48
shridutt kothari

是的,你可以像这样录制背景视频:

首先,使用服务创建视频应用。不要设置其视图以使其不可见。如果你在服务中这样做那就更好了,因为......

其次,您可以使用AlarmManager设置特定时间的警报,然后在此时,通过使用意图,再次启动您的服务。要停止您的应用程序,您也可以使用AlarmManager。

4
Rahul