android MediaRecorder在Service中获取预览帧而没有任何Activity

时间:2015-12-01 07:54:39

标签: android service camera mediarecorder

环境:

API等级16。

说明

我正在编写一个监控相机中任何移动的应用程序,例如任何人进入相机捕获区域。

然后我写了一个服务,它对我来说很好,它可以录制没有任何活动的视频:

public class Monitor extends Service {
    private Camera camera;
    private boolean recordingStatus;
    private MediaRecorder mediaRecorder;
    private File directory;

    @Override
    public void onCreate() {
        super.onCreate();
        recordingStatus = false;
    }

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);
        if (!recordingStatus) {
            startRecording();
        }
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        stopRecording();
        camera.release();
        recordingStatus = false;

        super.onDestroy();
    }

    public boolean startRecording() {
        try {
            recordingStatus = true;

            camera = Camera.open();
            camera.setDisplayOrientation(90);
            SurfaceTexture surfaceTexture = new SurfaceTexture(0);
            camera.setPreviewTexture(surfaceTexture);
            camera.setPreviewCallback(new Camera.PreviewCallback() {
                @Override
                public void onPreviewFrame(byte[] data, Camera camera) {
                    // never called here.
                }
            });
            camera.startPreview();
            camera.unlock();

            mediaRecorder = new MediaRecorder();
            mediaRecorder.setCamera(camera);
            mediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
         // "CamcorderProfile.QUALITY_TIME_LAPSE_LOW" not record audio
         // mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_TIME_LAPSE_LOW));

            directory = new File(Environment.getExternalStorageDirectory()
                    .toString() + "/videodemo/");
            if (!directory.exists())
                directory.mkdirs();

            long currentTime = System.currentTimeMillis();

            String uniqueOutFile = Environment.getExternalStorageDirectory()
                    .toString()
                    + "/videodemo/videooutput"
                    + currentTime
                    + ".mp4";
            File outFile = new File(directory, uniqueOutFile);
            if (outFile.exists()) {
                outFile.delete();
            }

            mediaRecorder.setOutputFile(uniqueOutFile);

            mediaRecorder.prepare();
            mediaRecorder.start();

            return true;
        } catch (IllegalStateException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    // Stop service
    public void stopRecording() {
        mediaRecorder.stop();
        mediaRecorder.release();
    }

}

问题

我想检测在Camera中移动的任何东西,所以我需要所有帧回调进行检测。但是" onPreviewFrame"从未打电话过 你能帮我解决一下吗?

0 个答案:

没有答案