如何从应用程序背景录制视频:Android
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10121660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to record video from background of application : Android
提问by Android Learner
I am developing an application which will be able to record video from background of application by using Service
.
我正在开发一个应用程序,它将能够使用Service
.
Problem description :
问题描述 :
In my application recording will be scheduled. If user want to record video from 1 PM to 3 PM, he will schedule the task and can exit from application. Application will automatically start recording at 1PM to 3PM.
在我的应用程序中将安排录制。如果用户想在下午 1 点到 3 点录制视频,他将安排任务并可以退出应用程序。应用程序将在下午 1 点至下午 3 点自动开始录制。
What I did yet :
我做了什么:
I googled about my query but didn't get solution. Many articles say that it is not possible. But in Google Playthere are some applications (for eg MyCar Recorder) which can record video from background of application.
我用谷歌搜索了我的查询,但没有得到解决方案。很多文章都说不可能。但是在Google Play 中有一些应用程序(例如MyCar Recorder)可以从应用程序的后台录制视频。
I got an articleabout same but its not working.
What is the way to implement this functionality?
实现此功能的方法是什么?
回答by shridutt kothari
1- I have created a activity to start service like this:
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 - Now I have created a service to record the video in backgroundlike this:
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;
}
}
It will create a file video.mp4 in your sd card. you may change the code for adding more functionality but the basic functionality is achieved through this code i.e. record video in background.
它将在您的 SD 卡中创建一个文件 video.mp4。您可以更改代码以添加更多功能,但基本功能是通过此代码实现的,即在后台录制视频。
NOTE: i have started the service through button click in activity but you can start it through any other way also like broadcastreceiver etc.
注意:我已经通过单击活动中的按钮启动了该服务,但您也可以通过任何其他方式启动它,例如广播接收器等。
Hope it helps!! Cheers.
希望能帮助到你!!干杯。
回答by Rahul
Yes, you can record the background video like this:
是的,您可以像这样录制背景视频:
First, create a video app using service. Do not set its view so that it will not be visible. If you are doing it in service then that is better because...
首先,使用服务创建一个视频应用程序。不要将其视图设置为不可见。如果你是在服务中这样做,那就更好了,因为......
Second, you can use the AlarmManager for setting the alarm of particular time and then at that time, by using intent, start your service again. For stopping your app you can use AlarmManager, as well.
其次,您可以使用AlarmManager 设置特定时间的警报,然后在该时间使用intent 重新启动您的服务。要停止您的应用程序,您也可以使用 AlarmManager。
回答by Arpit Agarwal
Here are some cool android projects regarding the same with some variations :
这里有一些很酷的 android 项目,但有一些变化:
https://github.com/zeitgeist87/SpartanTimeLapseRecorder/blob/master/src/at/andreasrohner/spartantimelapserec/recorder/VideoRecorder.java- Control FPS of camera
https://www.sisik.eu/blog/android/media/camera2-from-service- Image from background camera
https://www.sisik.eu/blog/android/media/camera2-from-service- 来自背景摄像头的图像
https://github.com/botyourbusiness/android-camera2-secret-picture-taker- Official
https://github.com/botyourbusiness/android-camera2-secret-picture-taker- 官方
https://gist.github.com/joseph-zhong/c2a52a5507d6588bec2db9869c860114
https://gist.github.com/joseph-zhong/c2a52a5507d6588bec2db9869c860114