Wednesday, March 26, 2014

Video on Android using MediaRecorder problem resolution


use MediaRecorder class, Camera recording function to develop Android-based phones

Access to video files is very vague, and then the system can not be achieved with a camera to record the effect of

After analysis, we found no auto-focus function when using video, resulting in poor video effects

But after adding the AF code, SurfaceView preview has the desired effect, but the problem is that the recorded video is Huaping

Have tangled several days, I hope to develop a large cattle Android can give help

Here is the code snippet

 

public class MainActivity extends Activity implements OnClickListener {

private Button video_record, video_stop;
private SurfaceView surfaceView;
private SurfaceHolder surfaceHolder;
private Camera camera;
private MediaRecorder mediaRecorder;

private boolean isCard, isFocus, isRecord;
private String sdCard, recordPath, recordFormat = "mp4.";

@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
getWindow () setFormat (PixelFormat.TRANSLUCENT);.
setContentView (R.layout.activity_main);
init ();
setListener ();
}

private void init () {
video_record = (Button) findViewById (R.id.video_record);
video_stop = (Button) findViewById (R.id.video_stop);
surfaceView = (SurfaceView) findViewById (R.id.video_surface);
surfaceHolder = surfaceView.getHolder ();
surfaceHolder.setType (SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
surfaceHolder.addCallback (new CustomCallBack ());
video_stop.setEnabled (false);

isCard = Environment.getExternalStorageState () equals (Environment.MEDIA_MOUNTED);.
isFocus = false;
isRecord = false;
if (isCard) {
sdCard = SDCardUtils.getExternalPath ();
}
}

private void setListener () {
video_record.setOnClickListener (this);
video_stop.setOnClickListener (this);
}

@ Override
public void onClick (View v) {
if (! isCard) {
toast ("SDCard is Invalid");
return;
}
switch (v.getId ()) {
case R.id.video_record:
record ();
break;
case R.id.video_stop:
stop ();
break;
}
}

private void record () {
isRecord = true;
try {
initCamera ();
initRecord ();
} Catch (IOException e) {
e.printStackTrace ();
}
referenceView ();
}

private void stop () {
isRecord = false;
freeRecordResource ();
freeCameraResource ();
referenceView ();
}

private void referenceView () {
if (isRecord) {
video_record.setEnabled (false);
video_stop.setEnabled (true);
} Else {
video_record.setEnabled (true);
video_stop.setEnabled (false);
recordPath = null;
}
}

private void initCamera () throws IOException {
if (camera! = null) {
freeCameraResource ();
}
try {
camera = Camera.open ();
} Catch (Exception e) {
e.printStackTrace ();
}
setCameraParams ();
camera.setDisplayOrientation (90);
camera.setPreviewDisplay (surfaceHolder);
camera.startPreview ();
if (isFocus) {
camera.autoFocus (null);
}
camera.unlock ();
}

private void initRecord () throws IOException {
if (mediaRecorder! = null) {
freeRecordResource ();
}
File file = createFile ();
mediaRecorder = new MediaRecorder ();
mediaRecorder.setCamera (camera);
mediaRecorder.setPreviewDisplay (surfaceHolder.getSurface ());
mediaRecorder.setVideoSource (VideoSource.CAMERA);
mediaRecorder.setAudioSource (AudioSource.MIC);
mediaRecorder.setOutputFormat (OutputFormat.MPEG_4);
mediaRecorder.setAudioEncoder (AudioEncoder.AMR_NB);
/ / MediaRecorder.setProfile (CamcorderProfile.get (CamcorderProfile.QUALITY_HIGH));
mediaRecorder.setVideoFrameRate (16);
mediaRecorder.setVideoEncodingBitRate (3000000);
/ / MediaRecorder.setOrientationHint (90);
mediaRecorder.setVideoEncoder (VideoEncoder.MPEG_4_SP);
/ / MediaRecorder.setMaxDuration (Constant.MAXVEDIOTIME * 1000);
mediaRecorder.setOutputFile (file.getAbsolutePath ());
mediaRecorder.prepare ();
mediaRecorder.start ();
}

private File createFile () throws IOException {
if (recordPath == null) {
recordPath = sdCard + File.separator + System.currentTimeMillis () + recordFormat;
}
File file = new File (recordPath);
if (! file.exists ()) {
File parent = file.getParentFile ();
if (! parent.exists ()) {
parent.mkdirs ();
}
file.createNewFile ();
}
return file;
}

private void freeCameraResource () {
if (camera! = null) {
camera.setPreviewCallback (null);
camera.stopPreview ();
camera.lock ();
camera.release ();
camera = null;
}
}

private void freeRecordResource () {
if (mediaRecorder! = null) {
mediaRecorder.stop ();
mediaRecorder.reset ();
mediaRecorder.release ();
mediaRecorder = null;
}
}

public void setCameraParams () {
if (camera! = null) {
Parameters params = camera.getParameters ();
List list = params.getSupportedFocusModes ();
if (list.contains (Parameters.FOCUS_MODE_AUTO)) {
isFocus = true;
params.setFocusMode (Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
}
params.set ("orientation", "portrait");
camera.setParameters (params);
}
}

@ Override
protected void onStop () {
stop ();
super.onStop ();
}

@ Override
protected void onDestroy () {
stop ();
super.onDestroy ();
}

private void toast (String text) {
Log.e ("custom", text);
Toast.makeText (this, text, Toast.LENGTH_LONG) show ();.
}

public class CustomCallBack implements Callback {

@ Override
public void surfaceCreated (SurfaceHolder holder) {
if (camera == null) {
try {
camera = Camera.open ();
camera.setDisplayOrientation (90);
camera.setPreviewDisplay (holder);
/ / Camera.startPreview ();
} Catch (IOException e) {
freeCameraResource ();
e.printStackTrace ();
}
}
}

@ Override
public void surfaceChanged (SurfaceHolder holder, int format, int width, int height) {
if (camera! = null) {
setCameraParams ();
camera.startPreview ();
if (isFocus) {
camera.autoFocus (null);
}
camera.unlock ();
}
}

@ Override
public void surfaceDestroyed (SurfaceHolder holder) {
if (camera! = null) {
freeCameraResource ();
}
}

}

}









Because the function is not yet complete, so no comment, willing to please help to help to see the big cattle
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
Will you set the profile bad?
Plus you commented mediaRecorder.setProfile (CamcorderProfile.get (CamcorderProfile.QUALITY_HIGH)); nor okay?
You put mediaRecorder.setVideoFrameRate (16); into 30 Try
Reply:

Test of time and there is no relationship between these two codes
 
mediaRecorder.setProfile (CamcorderProfile.get (CamcorderProfile.QUALITY_HIGH));
mediaRecorder.setVideoFrameRate (16);


If you remove the autofocus method is to record the video can be played, but the clarity poor

And some of the blog on the network in operation is also on the phone to record video of this effect

Coupled with the AF operation after a video recording of the video
Reply:

Test of time and there is no relationship between these two codes
 
mediaRecorder.setProfile (CamcorderProfile.get (CamcorderProfile.QUALITY_HIGH));
mediaRecorder.setVideoFrameRate (16);


If you remove the autofocus method is to record the video can be played, but the clarity poor

And some of the blog on the network in operation is also on the phone to record video of this effect

Coupled with the AF operation after a video recording of the video

cited a floor yuleyouxi reply: profile
will not you set it?
Plus you commented mediaRecorder.setProfile (CamcorderProfile.get (CamcorderProfile.QUALITY_HIGH)); nor okay?
You put mediaRecorder.setVideoFrameRate (16); into 30 to try

Reply:
Learn. . . How to deal with the back and clear the rate of compression ratio? ? ? ? ?
Reply:
AF code? Is this it?
if (list.contains (Parameters.FOCUS_MODE_AUTO)) {
isFocus = true;
params.setFocusMode (Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
}
There did not look to break out of this list? This code determines whether the current support auto focus mode, but the value is set continuous AF ah
Reply:
references, 5th Floor scott7667 reply:
autofocus code? Is this it?
if (list.contains (Parameters.FOCUS_MODE_AUTO)) {
isFocus = true;
params.setFocusMode (Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
}
There did not look to break out of this list? This code determines whether the current support auto focus mode, but the value is set continuous AF ah

I've given up ......
Reply:
Your video, click the Record button is the second time it appears it? Or a start recording appeared Huaping?
Reply:
You still can not clearly record it? The camera features with the phone itself, compared focusing function for you, they can not achieve a clear effect?
Reply:
reference to the 8th floor yong7356 reply:
you still can not clearly record it? The camera features with the phone itself, compared focusing function for you, they can not achieve a clear effect?


SurfaceView display content and the phone comes with nothing out of the gap, but the file is recorded Huaping

Now I use the phone comes with video recording function, Uri operation returns while getting recorded
Reply:
Huaping you because you wrote camera.startPreview ();, this sentence replaced camera.stopPreview (); on OK
Reply:
LZ solve it?
Reply:
I have encountered this problem, video size, and call the local cameras, but can not focus during recording, the clarity is poor. Could someone solve this problem?
Reply:
mediaRecorder.setPreviewDisplay (surfaceHolder.getSurface ());
The above sentence into mediaRecorder.setOutputFile (file.getAbsolutePath ()); behind to try;

No comments:

Post a Comment