Android 相机 - 从服务在后台运行

Posted

技术标签:

【中文标题】Android 相机 - 从服务在后台运行【英文标题】:Android Camera - Running in background from service 【发布时间】:2013-04-29 07:27:21 【问题描述】:

我一直在尝试在服务的后台运行摄像头。我已经按照How to use Camera to take picture in a background Service on android?、Taking picture from camera without preview中的方法进行了。这就是我所做的

ExperimentalControl.java

package com.example.test2;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class ExperimentControl extends Service 

     VideoRecorder vRecorder;   

     public ExperimentControl() 
     
         Log.i("EXPERIMENT","ExperimentControl started!!!");
     

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

     public int onStartCommand(Intent intent, int flags, int startId) 
     

        vRecorder = new VideoRecorder();
        vRecorder.startRecording(getApplicationContext());

        return START_STICKY;
     

     public void onDestroy()
     
        vRecorder=null;
     


VideoRecorder.java

package com.example.test2;

import java.io.IOException;
import android.content.Context;
import android.hardware.Camera;
import android.media.MediaRecorder;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class VideoRecorder implements SurfaceHolder.Callback 

    String videofolder = android.os.Environment.getExternalStorageDirectory()+"/Record/";
    private final String VIDEO_PATH_NAME = videofolder+"test.mp4";
    private MediaRecorder mMediaRecorder;
    private Camera mCamera;
    private SurfaceView mSurfaceView;
    private SurfaceHolder mHolder;
    private boolean mInitSuccesful;

    void startRecording(Context context)
    
        Log.v("Surya2","here");
        mSurfaceView = new SurfaceView(context);
        mHolder = mSurfaceView.getHolder();
        mHolder.addCallback(this);
        mMediaRecorder.start();
        try 
        
            Thread.sleep(10 * 1000);
         
        catch (Exception e) 
        
            e.printStackTrace();
        
        //finish();
        Log.v("Surya4","here");
    

    private void initRecorder(Surface surface) throws IOException 
    

        if(mCamera == null) 
        
            mCamera = Camera.open();
            mCamera.unlock();
        

        if(mMediaRecorder == null) 
            mMediaRecorder = new MediaRecorder();

        mMediaRecorder.setPreviewDisplay(surface);
        mMediaRecorder.setCamera(mCamera);
        mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
        mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
        mMediaRecorder.setVideoEncodingBitRate(512 * 1000);
        mMediaRecorder.setVideoFrameRate(30);
        mMediaRecorder.setVideoSize(640, 480);
        mMediaRecorder.setOutputFile(VIDEO_PATH_NAME);

        try 
        
            mMediaRecorder.prepare();
         
        catch (IllegalStateException e)
        
            e.printStackTrace();
        

        mInitSuccesful = true;
    

    @Override
    public void surfaceCreated(SurfaceHolder holder) 
    
        try 
        
            Log.v("Surya3","here");

            if(!mInitSuccesful)
                initRecorder(mHolder.getSurface());
         
        catch (IOException e) 
        
            e.printStackTrace();
        
    

    @Override
    public void surfaceDestroyed(SurfaceHolder holder)
    
        shutdown();
    

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) 

    private void shutdown() 
    
        // Release MediaRecorder and especially the Camera as it's a shared
        // object that can be used by other applications
        mMediaRecorder.stop();
        mMediaRecorder.reset();
        mMediaRecorder.release();
        mCamera.release();

        // once the objects have been released they can't be reused
        mMediaRecorder = null;
        mCamera = null;
    

Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.test2"
    android:versionCode="1"
     android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-feature android:name="android.hardware.camera.autofocus" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <service android:name=".ExperimentControl" />
</application>

</manifest>

这种方式正确吗?我面临的主要问题是这没有显示任何错误,也没有出现日志输出。所以我无法确定导致问题的原因,但没有录制视频。有人可以纠正我吗?

提前致谢

【问题讨论】:

【参考方案1】:

尝试使用Service上下文调用startrecording

vRecorder.startRecording(this);

您应该避免使用 ApplicationContext 来创建视图。当我尝试基于 ApplicationContext 创建 AlertDialog 时遇到了类似的问题。没有显示错误,但现在显示了对话框。

希望对您有所帮助!

【讨论】:

以上是关于Android 相机 - 从服务在后台运行的主要内容,如果未能解决你的问题,请参考以下文章

Android在后台运行任务时关闭屏幕

在后台捕获相机帧(Android)

在后台服务中拦截 Android 蓝牙相机快门

当我的应用程序在后台运行时,可以从摄像头获取通知吗?

在Android O中运行后台运行Intentservice

如何在Android中保持kivy服务在后台运行(切换到其他应用程序或锁定屏幕时服务仍然运行)?