Saving video files without overwriting the previous video??

Put your problem here if it does not fit any of the other categories.

Saving video files without overwriting the previous video??

Postby imso » Thu Mar 31, 2011 3:39 pm

How am i going to get the videos i recorded on my android device to save its file in an file folder classified and name by date/time for each individual video file.. Which look something like this in this link http://www.androidzoom.com/android_applications/travel_and_local/autoguard-blackbox_llix_screenshots.html where a list of video files are categorized. My previous video constantly got overwritten by another newer video file, which is not what i want, can someone tell me what i modify or add into my codes to get this done? I'm kinna new in java and eclipse therefore kinna bad in programming..

Code: Select all
package net.learn2develop.CameraTest;

import java.io.File;
import java.io.IOException;
import android.app.Activity;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Button;
import android.view.View;
import android.widget.Toast;

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;

   File tempFile = null;
   
    @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();
              Toast.makeText(getBaseContext(), "Recording Started", Toast.LENGTH_SHORT).show();
           }
           
        });
       
        Button btnStop = (Button) findViewById(R.id.button5);
        btnStop.setOnClickListener(new View.OnClickListener()
        {
           public void onClick(View v)
           {
              stopRecording();
              Toast.makeText(getBaseContext(), "Recording Stopped", Toast.LENGTH_SHORT).show();   
           }
        });
    }
    @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: Saving video files without overwriting the previous vide

Postby vhalexxs » Fri Apr 01, 2011 8:35 am

Hi imso,

tempFile = new File(Environment.getExternalStorageDirectory(),"1.3gp"); -- just change the "1.3gp" into something unique. e.g. System.currentTimeMillis() + ".3gp" or UUID.randomUUID().toString().replace("-","") + ".3gp"
Alex Escobar
Senior Android Engineer
-----------------------
Airpush.com: The Push Notification Ad Network
User avatar
vhalexxs
Developer
Developer
 
Posts: 28
Joined: Mon Mar 14, 2011 3:19 am

Re: Saving video files without overwriting the previous vide

Postby imso » Fri Apr 01, 2011 8:56 am

But the solution your suggested does not seems to provide the format in dateTime look similar to the image which provided in the link above? Can you help i'm kinna new in java and eclipse to think of a solution...
imso
Developer
Developer
 
Posts: 49
Joined: Mon Mar 21, 2011 10:47 am

Re: Saving video files without overwriting the previous vide

Postby strider2023 » Fri Apr 01, 2011 9:18 am

Hi imso,

You can use Java's SimpleDateFormat to specify the date and time that has to be embedded to you video name.
strider2023
Senior Developer
Senior Developer
 
Posts: 126
Joined: Tue Mar 02, 2010 6:34 am
Location: Chennai, India

Re: Saving video files without overwriting the previous vide

Postby imso » Fri Apr 01, 2011 9:25 am

But how should i come about doing it with simpleDateFormat? Tried reading online sample on datetime, something like 2011-04/01 16:49:15 but still don't really know how to integrate into my codes.. Sorry i don't really understand with this method?
imso
Developer
Developer
 
Posts: 49
Joined: Mon Mar 21, 2011 10:47 am

Re: Saving video files without overwriting the previous vide

Postby strider2023 » Mon Apr 04, 2011 5:34 am

Here is how,

private SimpleDateFormat videoTimeFormatter;
private Date videoTime;
private String timeString;
...

onCreate() {
videoTimeFormatter = new SimpleDateFormat("HH:mm:ss");
videoTime = new Date();
videoDate = Calendar.getInstance();
timeString = videoTimeFormatter.format(videoTime);
}
...
//then while saving video
tempFile = new File(Environment.getExternalStorageDirectory(),"MyPlayerVideo_" + timeString + ".3gp");
strider2023
Senior Developer
Senior Developer
 
Posts: 126
Joined: Tue Mar 02, 2010 6:34 am
Location: Chennai, India

Top

Re: Saving video files without overwriting the previous vide

Postby imso » Mon Apr 04, 2011 5:39 am

I tried out an alternative but how come it seems like the application was force to shut down?? Solutions

Code: Select all
  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();
   
                tempFile = new File(Environment.getExternalStorageDirectory(), dateFormat.format(date) + ".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;
       }
    }
imso
Developer
Developer
 
Posts: 49
Joined: Mon Mar 21, 2011 10:47 am

Re: Saving video files without overwriting the previous vide

Postby strider2023 » Mon Apr 04, 2011 6:40 am

What does the log say??
strider2023
Senior Developer
Senior Developer
 
Posts: 126
Joined: Tue Mar 02, 2010 6:34 am
Location: Chennai, India

Re: Saving video files without overwriting the previous vide

Postby imso » Mon Apr 04, 2011 7:26 am

How should i state for my log file in red? Because there's quite a lot of red errors...
imso
Developer
Developer
 
Posts: 49
Joined: Mon Mar 21, 2011 10:47 am

Re: Saving video files without overwriting the previous vide

Postby strider2023 » Mon Apr 04, 2011 7:38 am

Yup the red ones...btw one quick guess i think you are getting the error because you have a space between the date and time...try to bridge the gap with an underscore or something...
strider2023
Senior Developer
Senior Developer
 
Posts: 126
Joined: Tue Mar 02, 2010 6:34 am
Location: Chennai, India

Re: Saving video files without overwriting the previous vide

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

I found out that when i format in DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); it gets error but when format it in DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH_mm_ss"); it works...
But i wanted in semicol how should i do it?
imso
Developer
Developer
 
Posts: 49
Joined: Mon Mar 21, 2011 10:47 am

Re: Saving video files without overwriting the previous vide

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

And by the way could you take a look at another post related to "How do i implement background video recording in android?" in my recent another post under multimedia section of the forum. Which has another more difficult problem i presume.. Sorry for troubling you because barely no one goes to that section to help me out, Thank you :)
imso
Developer
Developer
 
Posts: 49
Joined: Mon Mar 21, 2011 10:47 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 9 guests