Android 4.0中的背景视频录制

时间:2013-02-24 05:43:24

标签: android video background recording

我尝试在Android 4.0中组织背景视频录制。但由于这些问题我无法做到:

  1. 虚拟表面在MediaRecorder中不起作用(错误:表面无效)

  2. 如果在“活动”上使用“Surface 1 x 1 px”,则“活动暂停时会消耗Surface”(停止录制)

  3. 如果在WindowsManager上使用Surface 1 x 1 px,则在应用程序暂停时停止Surface(停止录制)

  4. SurfaceTexture在MediaRecorder.setPreviewDisplay(新曲面(SurfaceTexture))中不起作用

  5. 窗口小部件不允许处理Surface 1 x 1 px

  6. 状态栏不允许处理Surface 1 x 1 px

  7. 请帮我找到正确的方法。

3 个答案:

答案 0 :(得分:41)

示例和简单代码(在Jelly Bean,SGS2上测试):

public class BackgroundVideoRecorder extends Service implements SurfaceHolder.Callback {

    private WindowManager windowManager;
    private SurfaceView surfaceView;
    private Camera camera = null;
    private MediaRecorder mediaRecorder = null;

    @Override
    public void onCreate() {

        // Start foreground service to avoid unexpected kill
        Notification notification = new Notification.Builder(this)
            .setContentTitle("Background Video Recorder")
            .setContentText("")
            .setSmallIcon(R.drawable.ic_launcher)
            .build();
        startForeground(1234, notification);

        // Create new SurfaceView, set its size to 1x1, move it to the top left corner and set this service as a callback
        windowManager = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
        surfaceView = new SurfaceView(this);
        LayoutParams layoutParams = new WindowManager.LayoutParams(
            1, 1,
            WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
            WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
            PixelFormat.TRANSLUCENT
        );
        layoutParams.gravity = Gravity.LEFT | Gravity.TOP;
        windowManager.addView(surfaceView, layoutParams);
        surfaceView.getHolder().addCallback(this);

    }

    // Method called right after Surface created (initializing and starting MediaRecorder)
    @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {

        camera = Camera.open();
        mediaRecorder = new MediaRecorder();
        camera.unlock();

        mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());
        mediaRecorder.setCamera(camera);
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
        mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));

        mediaRecorder.setOutputFile(
                Environment.getExternalStorageDirectory()+"/"+
                DateFormat.format("yyyy-MM-dd_kk-mm-ss", new Date().getTime())+
                ".mp4"
        );

        try { mediaRecorder.prepare(); } catch (Exception e) {}
        mediaRecorder.start();

    }

    // Stop recording and remove SurfaceView
    @Override
    public void onDestroy() {

        mediaRecorder.stop();
        mediaRecorder.reset();
        mediaRecorder.release();

        camera.lock();
        camera.release();

        windowManager.removeView(surfaceView);

    }

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

    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {}

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

}

不要忘记权限:

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

答案 1 :(得分:2)

try { 
        mediaRecorder.prepare(); 
    } catch (Exception e) {}
    mediaRecorder.start();
    Timer t = new Timer();
    t.schedule(new TimerTask() {
        @Override
        public void run() {
            stopSelf();
        }
    }, 5000);
}catch(Exception e){}

只需对上面的代码进行一些小修改......它会将5秒文件保存到你的sdcard中的根文件夹中...根据你的需要修改计时器。它也适用于Nexus 4和Micromax ..

答案 2 :(得分:-4)

我找到了答案:有必要使用WindowManager并从Service中调用它。