gpt4 book ai didi

android - Android 4.0 中的后台视频录制

转载 作者:IT老高 更新时间:2023-10-28 23:04:22 28 4
gpt4 key购买 nike

我尝试在 Android 4.0 中组织后台视频录制。但是因为这些问题我做不到:

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

  2. 如果在 Activity 上使用 Surface 1 x 1 px,Surface 在 Activity 暂停时被销毁(记录停止)

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

  4. SurfaceTexture 在 MediaRecorder.setPreviewDisplay(new Surface(SurfaceTexture)) 中不起作用

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

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

请帮我找到正确的方法。

最佳答案

示例和简单代码(在 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" />

关于android - Android 4.0 中的后台视频录制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15049041/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com