Problems recording video

All your problems with Audio, Video and Images.

Problems recording video

Postby underminedsk » Wed Jun 23, 2010 6:43 pm

Hi All,

I am having a few problems recording video. I will try to spell them out and include code:

1. I'm having alot of problems starting my video recording sessions. For example, if I do something like call videoRecorder.start() from my onCreate method, my program crashes and gives me some weird output in the logcat. However, if I call it from a trackball event or something similar, it works fine.

2. How do I make it so I can see the camera view when the VideoRecorder is not recording? Right now, it shows a black screen when not recording.

3. If I dont set the Activity that creates the videoRecorder instance as my startup activity, the VideoRecorder doesn't work.


Here is my code for the RecordingActivity:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class RecordingActivity extends Activity {
  2.         public static final String TAG = "[ - RecordingActivity - ]:";
  3.         VideoRecorder videoRecorder;
  4.         AtomizedIOManager ioManager;
  5.         protected void onCreate(Bundle savedInstanceState) {
  6.         super.onCreate(savedInstanceState);
  7.        
  8.         // Hide the window title.
  9.         requestWindowFeature(Window.FEATURE_NO_TITLE);    
  10.         getWindow().setFormat(PixelFormat.TRANSLUCENT);
  11.                 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
  12.  
  13.        
  14.         videoRecorder = new VideoRecorder(this);
  15.         ioManager = new AtomizedIOManager(Scanner3D.port, Scanner3D.IP, Scanner3D.port);
  16.        
  17.        
  18.         addContentView(videoRecorder, new LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
  19.                                                                                                            ViewGroup.LayoutParams.FILL_PARENT));
  20.                
  21.        videoRecorder.start();
  22.        }
  23. }
  24.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


My code for my VideoRecorder (copied mostly from the MediaRecorder Example):
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import java.io.File;
  2. import java.io.IOException;
  3.  
  4. import android.content.Context;
  5. import android.media.MediaRecorder;
  6. import android.os.Environment;
  7. import android.util.Log;
  8. import android.view.SurfaceHolder;
  9. import android.view.SurfaceView;
  10. import android.widget.Toast;
  11.  
  12. public class VideoRecorder extends SurfaceView{
  13.  
  14.           final String TAG = "[ - VideoRecorder - ]:";
  15.          
  16.           MediaRecorder recorder;
  17.           public boolean isRecording;
  18.           public String latestVideoPath;
  19.           public String path = "myVideo";
  20.           public SurfaceHolder previewHolder;
  21.           public int counter = 0;
  22.           Context ctx;
  23.  
  24.           /**
  25.            * Creates a new audio recording at the given path (relative to root of SD card).
  26.            */
  27.           public VideoRecorder(Context ctx) {
  28.                   super(ctx);
  29.                   this.ctx = ctx;
  30.               this.path = sanitizePath(path);
  31.               isRecording = false;
  32.            
  33.               previewHolder = this.getHolder();
  34.               previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  35.               previewHolder.addCallback(surfaceHolderListener);
  36.              
  37.                 recorder = new MediaRecorder();
  38.  
  39.            
  40.           }
  41.  
  42.           private String sanitizePath(String path) {
  43.             if (!path.startsWith("/")) {
  44.               path = "/" + path;
  45.             }
  46.             if (!path.contains(".")) {
  47.               path += ".3gp";
  48.             }
  49.             return Environment.getExternalStorageDirectory().getAbsolutePath() + path;
  50.           }
  51.  
  52.           /**
  53.            * Starts a new recording.
  54.            */
  55.           public void start() throws IOException {
  56.                     String state = android.os.Environment.getExternalStorageState();
  57.                     if(!state.equals(android.os.Environment.MEDIA_MOUNTED))  {
  58.                         Toast.makeText(ctx, "Error: SD card is not mounted - cannot record video.", Toast.LENGTH_SHORT).show();
  59.                         throw new IOException("SD Card is not mounted.  It is " + state + ".");
  60.                     }
  61.        
  62.                     // make sure the directory we plan to store the recording in exists
  63.                     File directory = new File(path).getParentFile();
  64.                     if (!directory.exists() && !directory.mkdirs()) {
  65.                       throw new IOException("Path to file could not be created.");
  66.                     }
  67.                    
  68.                     recorder.setPreviewDisplay(previewHolder.getSurface());
  69.                     recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
  70.                     recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);  
  71.                     recorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
  72.                     recorder.setVideoSize(640, 480);
  73.                    
  74.                    
  75.                     latestVideoPath = path + counter++;
  76.                    
  77.                     recorder.setOutputFile(latestVideoPath);
  78.  
  79.                    
  80.                     recorder.prepare(); recorder.start();
  81.                    
  82.                    
  83.                     Log.v(TAG, "starting a video recording...");
  84.                     isRecording = true;
  85.           }
  86.  
  87.           /**
  88.            * Stops a recording that has been previously started.
  89.            */
  90.           public void stop() throws IOException {
  91.                 isRecording = false;
  92.             recorder.stop();
  93.             Log.v(TAG, "stopping video recording...");
  94.             //recorder.release();
  95.           }
  96.          
  97.           private SurfaceHolder.Callback surfaceHolderListener = new SurfaceHolder.Callback() {
  98.                     public void surfaceCreated(SurfaceHolder holder) {
  99.                         try {
  100.                         } catch (Exception t) {
  101.                                 t.printStackTrace();
  102.                         };
  103.                     }
  104.                    
  105.                         public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
  106.                         {
  107.                          }
  108.                            
  109.                          public void surfaceDestroyed(SurfaceHolder arg0)
  110.                          {
  111.                                    Log.v(TAG, "Surface Destroyed...");
  112.                          }
  113.             };
  114. }
  115.  
Parsed in 0.045 seconds, using GeSHi 1.0.8.4
underminedsk
Developer
Developer
 
Posts: 26
Joined: Tue Mar 02, 2010 3:36 am

Top

Re: Problems recording video

Postby underminedsk » Thu Jun 24, 2010 4:46 am

Another Problem:

I have the following code, which produces a null pointer exception on the line " mSurfaceHolder = mVideoPreview.getHolder();", from the of onCreate here:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.           public void onCreate(Bundle extras) {
  2.               this.path = sanitizePath(path);
  3.               recorderRecording = false;
  4.            
  5.             //  setContentView(omniar.demo.R.id.video_camera);
  6.               mVideoPreview = (SurfaceView) findViewById(omniar.demo.R.id.camera_preview);
  7.              
  8.              
  9.               mSurfaceHolder = mVideoPreview.getHolder();
  10.               mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  11.               mSurfaceHolder.addCallback(this);
  12.                
  13.                 try { startPreviewThread.join();}catch (InterruptedException ex) { }
  14.           }
  15.        
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


However, the VideoCamera class from the android camera application source code does not throw an exception:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.     public void onCreate(Bundle icicle) {
  2.         super.onCreate(icicle);
  3.  
  4.         Window win = getWindow();
  5.  
  6.         // Overright the brightness settings if it is automatic
  7.         int mode = Settings.System.getInt(
  8.                 getContentResolver(),
  9.                 Settings.System.SCREEN_BRIGHTNESS_MODE,
  10.                 Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
  11.         if (mode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {
  12.             WindowManager.LayoutParams winParams = win.getAttributes();
  13.             winParams.screenBrightness = DEFAULT_CAMERA_BRIGHTNESS;
  14.             win.setAttributes(winParams);
  15.         }
  16.  
  17.         mPreferences = PreferenceManager.getDefaultSharedPreferences(this);
  18.         CameraSettings.upgradePreferences(mPreferences);
  19.  
  20.         readVideoPreferences();
  21.  
  22.         /*
  23.          * To reduce startup time, we start the preview in another thread.
  24.          * We make sure the preview is started at the end of onCreate.
  25.          */
  26.         Thread startPreviewThread = new Thread(new Runnable() {
  27.             public void run() {
  28.                 try {
  29.                     mStartPreviewFail = false;
  30.                     startPreview();
  31.                 } catch (CameraHardwareException e) {
  32.                     // In eng build, we throw the exception so that test tool
  33.                     // can detect it and report it
  34.                     if ("eng".equals(Build.TYPE)) {
  35.                         throw new RuntimeException(e);
  36.                     }
  37.                     mStartPreviewFail = true;
  38.                 }
  39.             }
  40.         }).start();
  41.  
  42.         mContentResolver = getContentResolver();
  43.  
  44.         requestWindowFeature(Window.FEATURE_PROGRESS);
  45.         setContentView(R.layout.video_camera);
  46.  
  47.         mPreviewFrameLayout = (PreviewFrameLayout) findViewById(R.id.frame_layout);
  48.         mPreviewFrameLayout.setOnSizeChangedListener(this);
  49.         resizeForPreviewAspectRatio();
  50.  
  51.         mVideoPreview = (SurfaceView) findViewById(R.id.camera_preview);
  52.         mVideoFrame = (ImageView) findViewById(R.id.video_frame);
  53.  
  54.         // don't set mSurfaceHolder here. We have it set ONLY within
  55.         // surfaceCreated / surfaceDestroyed, other parts of the code
  56.         // assume that when it is set, the surface is also set.
  57.         SurfaceHolder holder = mVideoPreview.getHolder();
  58.         holder.addCallback(this);
  59.         holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  60.  
  61.         mIsVideoCaptureIntent = isVideoCaptureIntent();
  62.         mQuickCapture = getIntent().getBooleanExtra(EXTRA_QUICK_CAPTURE, false);
  63.         mRecordingTimeView = (TextView) findViewById(R.id.recording_time);
  64.  
  65.         ViewGroup rootView = (ViewGroup) findViewById(R.id.video_camera);
  66.         LayoutInflater inflater = this.getLayoutInflater();
  67.         if (!mIsVideoCaptureIntent) {
  68.             View controlBar = inflater.inflate(
  69.                     R.layout.camera_control, rootView);
  70.             mLastPictureButton =
  71.                     (ImageView) controlBar.findViewById(R.id.review_thumbnail);
  72.             mThumbController = new ThumbnailController(
  73.                     getResources(), mLastPictureButton, mContentResolver);
  74.             mLastPictureButton.setOnClickListener(this);
  75.             mThumbController.loadData(ImageManager.getLastVideoThumbPath());
  76.             mSwitcher = ((Switcher) findViewById(R.id.camera_switch));
  77.             mSwitcher.setOnSwitchListener(this);
  78.             mSwitcher.addTouchView(findViewById(R.id.camera_switch_set));
  79.         } else {
  80.             View controlBar = inflater.inflate(
  81.                     R.layout.attach_camera_control, rootView);
  82.             controlBar.findViewById(R.id.btn_cancel).setOnClickListener(this);
  83.             ImageView retake =
  84.                     (ImageView) controlBar.findViewById(R.id.btn_retake);
  85.             retake.setOnClickListener(this);
  86.             retake.setImageResource(R.drawable.btn_ic_review_retake_video);
  87.             controlBar.findViewById(R.id.btn_play).setOnClickListener(this);
  88.             controlBar.findViewById(R.id.btn_done).setOnClickListener(this);
  89.         }
  90.  
  91.         ...button stuff here...
  92.  
  93.         // Make sure preview is started.
  94.         try {
  95.             startPreviewThread.join();
  96.             if (mStartPreviewFail) {
  97.                 showCameraBusyAndFinish();
  98.                 return;
  99.             }
  100.         } catch (InterruptedException ex) {
  101.             // ignore
  102.         }
  103.     }
Parsed in 0.050 seconds, using GeSHi 1.0.8.4


I cannot figure out the difference in getting a handle to the surfaceview.

Thoughts?
underminedsk
Developer
Developer
 
Posts: 26
Joined: Tue Mar 02, 2010 3:36 am

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: Google Feedfetcher and 8 guests