I am trying to start the camera in the background of a service. I followed the method shown in How to use the camera to shoot in the background on Android? , Shooting from the camera without preview . This is what I did
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(); }
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>
Is it correct? And the main problem that I am facing is that it does not show any errors and does not exit the log. So I canβt determine what causes the problem, but the video is not recorded. Can someone please correct me?
Thank you in advance
source share