Hi All,
Attached is my CustomVideoCameraExample that records video succsefuly using 
the rear camera, but fails when using the front one, the video is recorded 
with a green background and with no image at all, also when recording with 
the front camera the camera preview flickers, and in some cases crashes, all 
works correctly when using the rear camera.
I use Acer Iconia A500 (which is by the way a great device, and very 
developer friendly), with Honeycomb 3.1, build Acer_A500_4.0.10.11_COM_GEN2.
I tried most of the combinations of codecs, recording sizes etc, in some 
cases it totally crashes (device restart), in the best case it records the 
green screen.
My question is, does this issue is related to Honeycomb or is it device 
specific?
Did anyone managed to record from the front camera using another device the 
use Honeycomb 3.1?

Any help or insights will be greatly appreciated.

Thanks,
Eli

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
package com.pictell.mirror;

import java.io.File;
import java.io.IOException;
import java.util.List;

import android.app.Activity;

import android.util.Log;
import android.view.View.OnClickListener;
import android.content.pm.ActivityInfo;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

public class CustomVideoCameraExample extends Activity implements SurfaceHolder.Callback{

	

	private SurfaceView surfaceView;
	private SurfaceHolder surfaceHolder;
	private Camera camera;
	private boolean previewRunning;
	
	private MediaRecorder mediaRecorder;
	private final int maxDurationInMs = 20000;
	private final long maxFileSizeInBytes = 500000;
	private final int videoFramesPerSecond = 20;
	private boolean recording = false;
	// front =  1, rear = 0
	private int cameraType = 0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.custom_video_camera_example);
		surfaceView = (SurfaceView) findViewById(R.id.surface2);
		surfaceHolder = surfaceView.getHolder();
		surfaceHolder.addCallback(this);
		surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
		
		Button button = (Button)findViewById(R.id.Button01_rec_start_stop);
		button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Log.d(Prefs.TAG, "record start/ stop clicked");
				if (recording) {
					recording = false;
					stopRecording();
				} else {
					recording = true;
					if (startRecording4()) {
						Log.d(Prefs.TAG, "recording ...");
					}
					else {
						Log.d(Prefs.TAG, " recording failed.");
					}
				}
				
			}
		});
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		camera = Camera.open(cameraType);
		if (camera != null){
			Camera.Parameters p = camera.getParameters();
						
			camera.setParameters(p);
			camera.setDisplayOrientation(90);
		
			Log.d(Prefs.TAG, "Focus Mode: " + p.getFocusMode());
		}
		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();
		
		List<Camera.Size> list = p.getSupportedPreviewSizes ();
		//They are ordered from largest to smallest, so the largest will be...
		for (Camera.Size i : list) {
			Log.d(Prefs.TAG, "i.height: " + i.height + " i.width" + i.width);
		}
		Camera.Size size = list.get(0);

		p.setPreviewSize(480, 640);
		//p.setPreviewSize(size.width, size.height);
	
		camera.setParameters(p);

		try {
			camera.setPreviewDisplay(holder);
			camera.startPreview();
			camera.autoFocus(null);
			previewRunning = true;
		}
		catch (IOException e) {
			Log.e(Prefs.TAG,e.getMessage(), e);
			
		}
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		camera.stopPreview();
		previewRunning = false;
		camera.release();
	}
	
	public boolean startRecording(){
		try {
			Log.d(Prefs.TAG, "Start record...");
			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.setOutputFile("/sdcard/pictell.mp4");

			mediaRecorder.setVideoFrameRate(videoFramesPerSecond);
			
			mediaRecorder.setOrientationHint(90);
			//mediaRecorder.setVideoSize(480, 680);
			
			// works!
			//mediaRecorder.setVideoSize(176, 144); 
			
			//mediaRecorder.setVideoSize(240, 340); 
			//mediaRecorder.get

			mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
			mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
			mediaRecorder.setAudioChannels(1); 

			mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());

			mediaRecorder.setMaxFileSize(maxFileSizeInBytes);
			
			Log.d(Prefs.TAG, "Sleeping 1 secs");
			try {Thread.sleep(1000);} catch (InterruptedException e) {}
			Log.d(Prefs.TAG, "call prepare");
			
            mediaRecorder.prepare();
			mediaRecorder.start();

			return true;
		} catch (IllegalStateException e) {
			Log.e(Prefs.TAG,e.getMessage(), e);
			
			return false;
		} catch (IOException e) {
			Log.e(Prefs.TAG,e.getMessage(), e);
			
			return false;
		}
	}
	
	
	public boolean startRecording2(){
		try {
			Log.d(Prefs.TAG, "Start record...");
			camera.unlock();

			mediaRecorder = new MediaRecorder();

			mediaRecorder.setCamera(camera);
			mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
			mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
			mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
			//mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);

			// not working
			//mediaRecorder.setVideoSize(480, 640);
			
			// not working
			//CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
			//mediaRecorder.setProfile(profile);
			//mediaRecorder.setCaptureRate(20);
			
			//mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());
			mediaRecorder.setOutputFile("/sdcard/pictell.mpeg");
			//mediaRecorder.setMaxDuration(50000); // 50 seconds
			//mediaRecorder.setMaxFileSize(5000000); // Approximately 5 megabytes
			
			Thread.sleep(300);
            mediaRecorder.prepare();
			mediaRecorder.start();

			return true;
		
			
		} catch (IllegalStateException e) {
			Log.e(Prefs.TAG,e.getMessage(), e);
			return false;
		} catch (IOException e) {
			Log.e(Prefs.TAG,e.getMessage(), e);
			return false;
		} catch (Exception e) {
			Log.e(Prefs.TAG,e.getMessage(), e);
			return false;
		}
	}
	
	// this works with camera 0
	public boolean startRecording3(){
		try {
			Log.d(Prefs.TAG, "Start record...");
			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.setOutputFile("/sdcard/pictell.mpeg");

			mediaRecorder.setVideoFrameRate(30);
			//mediaRecorder.setVideoSize(480, 680);
			
			// works!
			//mediaRecorder.setVideoSize(176, 144); 
			
			//mediaRecorder.setVideoSize(240, 340); 
			//mediaRecorder.get

			mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
			mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
			mediaRecorder.setAudioChannels(1); 

			mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());

			mediaRecorder.setMaxFileSize(maxFileSizeInBytes);
			
			Log.d(Prefs.TAG, "Sleeping 1 secs");
			try {Thread.sleep(100);} catch (InterruptedException e) {}
			Log.d(Prefs.TAG, "call prepare");
			
            mediaRecorder.prepare();
			mediaRecorder.start();

			return true;
		} catch (IllegalStateException e) {
			Log.e(Prefs.TAG,e.getMessage(), e);
			
			return false;
		} catch (IOException e) {
			Log.e(Prefs.TAG,e.getMessage(), e);
			
			return false;
		}
	}
	
	
	// works good for the rear camera
	public boolean startRecording4(){
		try {
			Log.d(Prefs.TAG, "Start record...");
			camera.unlock();

			mediaRecorder = new MediaRecorder();
			//camera.unlock();
			mediaRecorder.setCamera(camera);
			mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
			mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
			mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);

			mediaRecorder.setMaxDuration(maxDurationInMs);

			
			mediaRecorder.setOutputFile("/sdcard/pictell.mpeg");

			mediaRecorder.setVideoFrameRate(15);
			mediaRecorder.setCaptureRate(15);
			
			mediaRecorder.setOrientationHint(90);
			
			// worked but paused and stuck the device
			//mediaRecorder.setVideoSize(320, 240);
			
			//mediaRecorder.setVideoSize(240, 340); 
			
			// not working
			//mediaRecorder.setVideoSize(1280, 720);
			
			// not working
			//mediaRecorder.setVideoSize(480, 640);
			
			// works!
			mediaRecorder.setVideoSize(640, 480);
			
			// works!
			//mediaRecorder.setVideoSize(176, 144); 
			
			//mediaRecorder.setVideoSize(240, 340); 
			//mediaRecorder.get

			mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
			// works but 264 works better
			//mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
			mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
			mediaRecorder.setAudioChannels(1); 

			mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());

			mediaRecorder.setMaxFileSize(maxFileSizeInBytes);
			
			Log.d(Prefs.TAG, "Sleeping to sync the cam");
			try {Thread.sleep(200);} catch (InterruptedException e) {}
			
			Log.d(Prefs.TAG, "call prepare");
			
            mediaRecorder.prepare();
			mediaRecorder.start();

			return true;
		} catch (IllegalStateException e) {
			Log.e(Prefs.TAG,e.getMessage(), e);
			
			return false;
		} catch (IOException e) {
			Log.e(Prefs.TAG,e.getMessage(), e);
			
			return false;
		} 
	}
	
	// using profile does not work good, preview and record get stuck.
	public boolean startRecording5(){
		try {
			Log.d(Prefs.TAG, "Start record...");
			camera.unlock();

			mediaRecorder = new MediaRecorder();
			//camera.unlock();
			mediaRecorder.setCamera(camera);
			mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
			mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
			//mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);

			// 20 secs
			mediaRecorder.setMaxDuration(20000);

						
			mediaRecorder.setOutputFile("/sdcard/pictell.mpeg");

			//mediaRecorder.setVideoFrameRate(30);
			//mediaRecorder.setCaptureRate(10);
			
			mediaRecorder.setOrientationHint(90);
			
			// not working
			//mediaRecorder.setVideoSize(480, 640);
			
			// works!
			//mediaRecorder.setVideoSize(640, 480);
			
			// works!
			//mediaRecorder.setVideoSize(176, 144); 
			
			//mediaRecorder.setVideoSize(240, 340); 
			//mediaRecorder.get

			//mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
			//mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
			//mediaRecorder.setAudioChannels(1); 

			mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());

			mediaRecorder.setMaxFileSize(maxFileSizeInBytes);
			
			CamcorderProfile profile = CamcorderProfile.get(cameraType, CamcorderProfile.QUALITY_LOW);
			mediaRecorder.setProfile(profile);
			
			Log.d(Prefs.TAG, "Sleeping to sync the cam");
			try {Thread.sleep(200);} catch (InterruptedException e) {}
			
			Log.d(Prefs.TAG, "call prepare");
			
            mediaRecorder.prepare();
			mediaRecorder.start();

			return true;
		} catch (IllegalStateException e) {
			Log.e(Prefs.TAG,e.getMessage(), e);
			
			return false;
		} catch (IOException e) {
			Log.e(Prefs.TAG,e.getMessage(), e);
			
			return false;
		} 
	}
	
	public void stopRecording(){
		Log.d(Prefs.TAG, "Stop record...");
		mediaRecorder.stop();
		mediaRecorder.reset(); 
		try {
			camera.reconnect();
		} catch (IOException e) {
			Log.d(Prefs.TAG, e.getMessage(), e);
		}
	}
	
	
}

Reply via email to