How do i implement background video recording in android?

All your problems with Audio, Video and Images.

How do i implement background video recording in android?

Postby imso » Mon Apr 04, 2011 9:08 am

How am i going to allow background video recording after i leave the application,or received an sms/call and when i'm come back its still recording and data was not lost because i'm creating an car blackbox android application??
I was given clue that i should be using services in a new .java file in order to invoke and extend to to do background recording, but i don't seems to know how to do it despite reading tons of sites related to services,please don't relate me to any other site anymore , i have read tons of it.. I tried using AsyncTask but me lecturer told me cannot... Can someone help me related to this topic whether is it using AsyncTask or services to perform background recording??

Code: Select all
public class CameraTest extends Activity implements SurfaceHolder.Callback {
   
   private static final String TAG ="CAMERA_TUTORIAL";
   
   private SurfaceView surfaceView;
   private SurfaceHolder surfaceHolder;
   private Camera camera;
   private boolean previewRunning;
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        surfaceView = (SurfaceView)findViewById(R.id.surface_camera);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);
       
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
       
        Button btnStart = (Button) findViewById(R.id.button4);
        btnStart.setOnClickListener(new View.OnClickListener()
        {
           public void onClick(View v)
           {
              startRecording();
           }
           
        });
       
        Button btnStop = (Button) findViewById(R.id.button5);
        btnStop.setOnClickListener(new View.OnClickListener()
        {
           public void onClick(View v)
           {
              stopRecording();
           }
           
        });
    }
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
       camera = Camera.open();
       if (camera != null) {
          Camera.Parameters params = camera.getParameters();
          camera.setParameters(params);
       }
       else {
          Toast.makeText(getApplicationContext(), "Camera not available!", Toast.LENGTH_LONG).show();
          finish();
       }
    }
   
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
       if (previewRunning) {
          camera.stopPreview();
       }
       Camera.Parameters p = camera.getParameters();
       p.setPreviewSize(320, 240);
       p.setPreviewFormat(PixelFormat.JPEG);
       camera.setParameters(p);
       
       try {
             camera.setPreviewDisplay(holder);
             camera.startPreview();
             previewRunning = true;
       }
       catch (IOException e) {
          Log.e(TAG,e.getMessage());
          e.printStackTrace();
       }
    }
   
    @Override
    public void surfaceDestroyed(SurfaceHolder holder){
          camera.stopPreview();
          previewRunning = false;
          camera.release();
    }
   
   
   
    private MediaRecorder mediaRecorder;
    private final int maxDurationInMs = 20000;
    private final int videoFramesPerSecond = 20;
   
    public boolean startRecording(){
       try {
             camera.unlock();
             
             mediaRecorder = new MediaRecorder();
             
             mediaRecorder.setCamera(camera);
             
             mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
   
             mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
   
             mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
                
             mediaRecorder.setMaxDuration(maxDurationInMs);
                
                
             
                
                mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
                
                mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
                
                //mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
                
                tempFile = new File(Environment.getExternalStorageDirectory(),"1.3gp");
                mediaRecorder.setOutputFile(tempFile.getPath());
                
                mediaRecorder.setVideoSize(surfaceView.getWidth(),surfaceView.getHeight());
                
                //mediaRecorder.setVideoFrameRate(videoFramesPerSecond);
                
                mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());
                
                            
                      mediaRecorder.prepare();
                      mediaRecorder.start();   
                      return true;
                            
       } catch (IllegalStateException e) {
          Log.e(TAG,e.getMessage());
          e.printStackTrace();
          return false;
       } catch (IOException e) {
          Log.e(TAG,e.getMessage());
          e.printStackTrace();
          return false;
       }
    }
   
    public void stopRecording(){
       mediaRecorder.stop();
       camera.lock();
    }
}
imso
Developer
Developer
 
Posts: 49
Joined: Mon Mar 21, 2011 10:47 am

Top

Re: How do i implement background video recording in android

Postby strider2023 » Tue Apr 05, 2011 8:00 am

Hi,

You can check one of my older posts. The method infolved there calls in the default camera software for recording your video. You can save it in your defined path and it will definitely save you a lot of pain... :)

post94133.html#p94133
strider2023
Senior Developer
Senior Developer
 
Posts: 126
Joined: Tue Mar 02, 2010 6:34 am
Location: Chennai, India

Re: How do i implement background video recording in android

Postby imso » Tue Apr 05, 2011 8:43 am

But the post you recommended seems to be for taking pictures? I was told by my lecturer that i should use service to do the background recording, when the user wanted to pick calls/sms etc... but i don't seems to know how to do this on another .java file to perform background recording.. Can you help me in this particular issue on creating the service class, sorry i'm kinna bad in android if i asked some stupid questions yet somehow you have directed me somehow...
imso
Developer
Developer
 
Posts: 49
Joined: Mon Mar 21, 2011 10:47 am

Re: How do i implement background video recording in android

Postby strider2023 » Tue Apr 05, 2011 9:26 am

Change the line

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

to
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
strider2023
Senior Developer
Senior Developer
 
Posts: 126
Joined: Tue Mar 02, 2010 6:34 am
Location: Chennai, India

Re: How do i implement background video recording in android

Postby imso » Tue Apr 05, 2011 9:42 am

You mean i should copy the codes and put it into my service class? if so it will not sync with my recording in my mainactivity.java file... Erm i'm getting lost.. I thought service should be entirely another thing to start/continue the recording at the background?
imso
Developer
Developer
 
Posts: 49
Joined: Mon Mar 21, 2011 10:47 am

Re: How do i implement background video recording in android

Postby strider2023 » Tue Apr 05, 2011 9:48 am

I will share a video capturing code with you...but you have to wait for another 6-7 hrs...i.e. aftr i get bak home :P
strider2023
Senior Developer
Senior Developer
 
Posts: 126
Joined: Tue Mar 02, 2010 6:34 am
Location: Chennai, India

Top

Re: How do i implement background video recording in android

Postby imso » Tue Apr 05, 2011 10:00 am

Thank you very much, i have been asking these question for weeks in other forum but no one answered my questions, thank you or else i will not be able to answer to my lecturer tomorrow about this background recording thingy :)
imso
Developer
Developer
 
Posts: 49
Joined: Mon Mar 21, 2011 10:47 am

Re: How do i implement background video recording in android

Postby imso » Wed Apr 06, 2011 7:23 am

I was told by my lecturer to have my startRecording() method be shift over to my service java file, RecordService.java file to handle all the Recording function and stopRecording function but how should i do recording using the service class?

This is what i have tried so far for my mainactivity.java file

Code: Select all
public class CameraTest extends Activity implements SurfaceHolder.Callback {
   
   private static final String TAG ="CAMERA_TUTORIAL";
   
   private SurfaceView surfaceView;
   private SurfaceHolder surfaceHolder;
   private Camera camera;
   private boolean previewRunning;
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        surfaceView = (SurfaceView)findViewById(R.id.surface_camera);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);
       
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
       
        Button btnStart = (Button) findViewById(R.id.button4);
        btnStart.setOnClickListener(new View.OnClickListener()
        {
           public void onClick(View v)
           {
              startService(new Intent(this, ServiceRecording.class));
           }
        });
       
        Button btnStop = (Button) findViewById(R.id.button5);
        btnStop.setOnClickListener(new View.OnClickListener()
        {
           public void onClick(View v)
           {
              stopRecording();
           }
        });
    }
   
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
       camera = Camera.open();
       if (camera != null) {
          Camera.Parameters params = camera.getParameters();
          camera.setParameters(params);
       }
       else {
          Toast.makeText(getApplicationContext(), "Camera not available!", Toast.LENGTH_LONG).show();
          finish();
       }
    }
   
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
       if (previewRunning) {
          camera.stopPreview();
       }
       Camera.Parameters p = camera.getParameters();
       p.setPreviewSize(320, 240);
       p.setPreviewFormat(PixelFormat.JPEG);
       camera.setParameters(p);
       
       try {
             camera.setPreviewDisplay(holder);
             camera.startPreview();
             previewRunning = true;
       }
       catch (IOException e) {
          Log.e(TAG,e.getMessage());
          e.printStackTrace();
       }
    }
   
    @Override
    public void surfaceDestroyed(SurfaceHolder holder){
        
          camera.stopPreview();
          previewRunning = false;
          camera.release();
    }
   
    public void stopRecording() {
       Toast.makeText(getBaseContext(), "Recording Stopped", Toast.LENGTH_SHORT).show();   
       mediaRecorder.stop();
       camera.lock();
    }
}


And this is my RecordService java file but is a piece of non-working/completed work... Can you tell me what i should do next?? I'm really lost not knowing what to do next to allow both java to work hand in hand.despite me reading tons of service website article??
Code: Select all
public class ServiceRecording extends Service {

   @Override
   public IBinder onBind(Intent intent) {
      // TODO Auto-generated method stub
      return null;
   }
   
   private MediaRecorder mediaRecorder;
   private SurfaceView surfaceView;
   private Camera camera;
   private boolean previewRunning;
   private SurfaceHolder surfaceHolder;
        private final int maxDurationInMs = 20000;
   
    @Override
    public void onCreate() {
    super.onCreate();
   
    }
   
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand();
   
    public boolean startRecording(){
       try {
             Toast.makeText(getBaseContext(), "Recording Started", Toast.LENGTH_SHORT).show();
             camera.unlock();
             
             mediaRecorder = new MediaRecorder();
             
             mediaRecorder.setCamera(camera);
             
             mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
   
             mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
   
             mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
                
             mediaRecorder.setMaxDuration(maxDurationInMs);
                
                
                mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
                
                mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
                
                //mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
                DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH_mm_ss");
                Date date = new Date();
                File directory = new File(Environment.getExternalStorageDirectory() + "/VideoList");
                
                if(!(directory.exists()))
                   directory.mkdir();
                   
                File FileSaved = new File(Environment.getExternalStorageDirectory() + "/VideoList", dateFormat.format(date) + ".3gp");
                mediaRecorder.setOutputFile(FileSaved.getPath());
                
                mediaRecorder.setVideoSize(surfaceView.getWidth(),surfaceView.getHeight());
                
                //mediaRecorder.setVideoFrameRate(videoFramesPerSecond);
                
                mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());
                         
                mediaRecorder.prepare();
                mediaRecorder.start();   
                return true;                  
       }
       
       catch (IllegalStateException e) {
          Log.e(TAG,e.getMessage());
          e.printStackTrace();
          return false;
       }
       catch (IOException e) {
          Log.e(TAG,e.getMessage());
          e.printStackTrace();
          return false;
       }
    }
   
    @Override
    public void onDestroy() {
    super.onDestroy();
   
    camera.stopPreview();
   previewRunning = false;
   camera.release();
    }   
   
    public void stopRecording() {
       Toast.makeText(getBaseContext(), "Recording Stopped", Toast.LENGTH_SHORT).show();   
       mediaRecorder.stop();
       camera.lock();
    }
}
imso
Developer
Developer
 
Posts: 49
Joined: Mon Mar 21, 2011 10:47 am

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: Google [Bot] and 9 guests