屏幕截图时Android应用程式录像 [英] Android app video recording when screen off

查看:158
本文介绍了屏幕截图时Android应用程式录像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是制作一个可以在屏幕关闭时录制视频或音频的应用程序。无论是我们开始录制,然后关闭屏幕,或者录音机应用语音或任何其他外部命令触发。在任何一种情况下,应用程序应该继续录制。



请提供代码。

解决方案

大多数情况下,当需要离线完成某件事情时,才能在服务中完成。




(www.slideshare.net)



假设您不知道什么是服务,请从文档中获取:


服务是代表
应用程序希望执行更长时间运行的操作,而不是
与用户交互或为其他
应用程序提供功能。每个服务类必须在其包的AndroidManifest.xml中具有相应的
声明。服务
可以使用Context.startService()和Context.bindService()启动。


所以基本上,它只是一些东西在后台运行,包括屏幕关闭时。现在,您必须做的所有事情都是将录制代码放在一起。您可能会遇到一个问题,即您的服务被系统杀死,就像android文档指定的那样:


注意这意味着大部分时间您的服务正在运行,如果系统内存压力很大,系统可能会遇到
。如果这个
发生,系统稍后会尝试重新启动该服务。一个
的重要后果是,如果您实现
onStartCommand()来排定异步处理工作或
另一个线程,则可能需要使用START_FLAG_REDELIVERY来获取
系统为您重新发送一个Intent,以便如果您的服务在处理它时被杀死,则不会丢失


在这种情况下,您需要添加一些代码以使服务保持在前台中,并确保不会被杀死:

  startForeground(); //保持服务不被破坏。 

这可能提供的唯一问题是在服务活着的时候不断的通知。 / p>

您还可以使用 startSticky()

  @Override 
public int onStartCommand(Intent intent,int flags,int startId){

//我们希望此服务继续运行,直到它明确
//停止,所以返回粘性。
返回START_STICKY;
}

现在,说完你的录音。你可以使用:

  stopSelf(); 

停止服务,或:

  Context.stopService(); 

此通知不是很大的问题,因为屏幕很可能在服务是运行。您还可以自定义您的通知,用户更了解后台正在发生的情况。



以下是一些示例录音代码:

  public class VideoCapture extends Activity implements OnClickListener,SurfaceHolder.Callback {
MediaRecorder recorder;
SurfaceHolder持有人;
boolean recording = false;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow()。setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

recorder = new MediaRecorder();
initRecorder();
setContentView(R.layout.main);

SurfaceView cameraView =(SurfaceView)findViewById(R.id.CameraView);
holder = cameraView.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

cameraView.setClickable(true);
cameraView.setOnClickListener(this);
}

private void initRecorder(){
recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);

摄像机配置文件cpHigh = CamcorderProfile
.get(CamcorderProfile.QUALITY_HIGH);
recorder.setProfile(cpHigh);
recorder.setOutputFile(/ sdcard / videocapture_example.mp4);
recorder.setMaxDuration(50000); // 50秒
recorder.setMaxFileSize(5000000); //大约5兆字节
}

private void prepareRecorder(){
recorder.setPreviewDisplay(holder.getSurface());

try {
recorder.prepare();
} catch(IllegalStateException e){
e.printStackTrace();
finish();
} catch(IOException e){
e.printStackTrace();
finish();
}
}

public void onClick(View v){
if(recording){
recorder.stop();
recording = false;

//让我们的initRecorder,所以我们可以再次记录
initRecorder();
prepareRecorder();
} else {
recording = true;
recorder.start();
}
}

public void surfaceCreated(SurfaceHolder holder){
prepareRecorder();
}

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

public void surfaceDestroyed (SurfaceHolder holder){
if(recording){
recorder.stop();
recording = false;
}
recorder.release();
finish();
}
}

如何在Android上拍摄录像?



再次,将代码放入长时间运行的前台服务中,如前所述。



如果您需要更多帮助,请随时问。



〜Ruchir


What I am trying to do is that make an app that can record video or audio while the screen is off. Either we start recording and then switch off the screen or the recorder should trigger with a voice or any other external command. In either case the app should keep on recording.

Please provide code of you can.

解决方案

Most of the time, when something needs to be done offline, it is done in a service.

(www.slideshare.net)

Assuming you don't know what a service is, from the docs:

A Service is an application component representing either an application's desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use. Each service class must have a corresponding declaration in its package's AndroidManifest.xml. Services can be started with Context.startService() and Context.bindService().

So basically, its just something that runs in the background, including when the screen is off. Now, all you have to do, is put your recording code in a service. You may face a problem that your service gets killed by the system, as the android docs specify:

Note this means that most of the time your service is running, it may be killed by the system if it is under heavy memory pressure. If this happens, the system will later try to restart the service. An important consequence of this is that if you implement onStartCommand() to schedule work to be done asynchronously or in another thread, then you may want to use START_FLAG_REDELIVERY to have the system re-deliver an Intent for you so that it does not get lost if your service is killed while processing it.

In this case, you will need to add certain code to keep the service in the foreground and make sure that it doesn't get killed:

startForeground();//keeps service from getting destroyed.

The only problem that this could provide is a constant notification for the time that the service is alive.

You may also use startSticky():

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

       // We want this service to continue running until it is explicitly
       // stopped, so return sticky.
       return START_STICKY;
}

Now, say you are done recording. You can use:

stopSelf();

to stop the service, or:

Context.stopService();

This notification is not much of a problem though, because the screen is most likely off while the service is running. You can also customize your notification, and it is better for the user to know what is going on in the background.

Here's some sample recording code:

public class VideoCapture extends Activity implements OnClickListener, SurfaceHolder.Callback {
    MediaRecorder recorder;
    SurfaceHolder holder;
    boolean recording = false;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        recorder = new MediaRecorder();
        initRecorder();
        setContentView(R.layout.main);

        SurfaceView cameraView = (SurfaceView) findViewById(R.id.CameraView);
        holder = cameraView.getHolder();
        holder.addCallback(this);
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        cameraView.setClickable(true);
        cameraView.setOnClickListener(this);
    }

    private void initRecorder() {
        recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
        recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);

        CamcorderProfile cpHigh = CamcorderProfile
                .get(CamcorderProfile.QUALITY_HIGH);
        recorder.setProfile(cpHigh);
        recorder.setOutputFile("/sdcard/videocapture_example.mp4");
        recorder.setMaxDuration(50000); // 50 seconds
        recorder.setMaxFileSize(5000000); // Approximately 5 megabytes
    }

    private void prepareRecorder() {
        recorder.setPreviewDisplay(holder.getSurface());

        try {
            recorder.prepare();
        } catch (IllegalStateException e) {
            e.printStackTrace();
            finish();
        } catch (IOException e) {
            e.printStackTrace();
            finish();
        }
    }

    public void onClick(View v) {
        if (recording) {
            recorder.stop();
            recording = false;

            // Let's initRecorder so we can record again
            initRecorder();
            prepareRecorder();
        } else {
            recording = true;
            recorder.start();
        }
    }

    public void surfaceCreated(SurfaceHolder holder) {
        prepareRecorder();
    }

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

    public void surfaceDestroyed(SurfaceHolder holder) {
        if (recording) {
            recorder.stop();
            recording = false;
        }
        recorder.release();
        finish();
    }
}

(How can I capture a video recording on Android?)

Again, put that code into a long running foreground service, as I explained earlier.

If you need any more help, feel free to ask.

~Ruchir

这篇关于屏幕截图时Android应用程式录像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆